为什么我不能在javascript中为字符串对象添加属性? [英] Why can't I add properties to a string object in javascript?

查看:135
本文介绍了为什么我不能在javascript中为字符串对象添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了另一个开发人员写的一些javascript代码。他不喜欢我们在整个项目中使用的网格组件,因此他决定编写自己的网格组件。他写的网格不能对日期进行排序,因为它只能绑定到字符串/数字。在使用之前,他将所有日期转换为字符串。我查看了他编写的日期函​​数的字符串格式,并认为我可以只使用原始值为字符串添加日期属性,然后在排序时查看字符串是否具有日期属性并基于此排序。但是,似乎您无法在javascript中向字符串添加属性。我不知道有些类型你不能添加属性。例如:

I inherited some javascript code another developer wrote. He didn't like the grid component we used throughout the project, so he decided to write his own. The grid he wrote can't sort dates, because it can only bind to strings / numbers. He converts all dates to strings before using them. I looked at the string formatting of date function he wrote, and figured I could just add a date property to the string with the original value, and then when sorting see if the string has a date property and sort based on that. However, it seems like you can't add properties to strings in javascript. I wasn't aware there were certain types you can't add properties to. For example:

<html>
<script>
var test = "test";
test.test = "test inner";
console.log(test);
console.log(test.test);
</script>

test.test将是未定义的。奇怪的。我的问题是为什么这段代码不起作用?而且,如果你能想到在网格上排序日期的任何变通方法(除了实际绑定到日期对象而不是字符串,这将很难修复),这将非常有用。

test.test will be undefined. Weird. My question is why this code doesn't work? And also, if you can think of any workarounds for sorting dates on that grid (besides actually binding to date objects instead of strings, which would be a pain to fix,) that would be really helpful.

推荐答案

JavaScript中有6种语言类型:

There are 6 language types in JavaScript:


  • 5种原始类型: String Number Boolean Null Undefined

  • 1非原始类型:对象

  • 5 primitive types: String, Number, Boolean, Null, Undefined
  • 1 non-primitive type: Object

原始类型的值被调用原始值,它们不能具有属性。

Object 非基本类型的值被称为对象,它们可以具有属性。

Values of the primitive types are called primitive values and they cannot have properties.
Values of the Object non-primitive type are called objects an they can have properties.

当您尝试将名为'bar'的属性分配给变量 foo ,如下所示:

When you try to assign a property named 'bar' to a variable foo, like so:

foo.bar = 'abc';

那么结果将取决于 foo的值的类型

(a)如果 foo 的值是 Undefined Null 的类型,然后会抛出错误,

(a) if the value of foo is of the type Undefined or Null, then an error will be thrown,

(b)如果 foo 的值属于 Object 类型,则命名属性'bar'将在对象 foo 上定义(如有必要),其值将设置为'abc'

(b) if the value of foo is of the type Object, then a named property 'bar' will be defined on the object foo (if necessary), and its value will be set to 'abc',

(c)如果 foo 的值属于该类型 Number String Boolean ,然后变量 foo 将不会更改办法。在这种情况下,上述分配操作将是 noop

(c) if the value of foo is of the type Number, String or Boolean, then the variable foo will not be changed in any way. In this case, the above assignment operation will be a noop.

因此,正如您所看到的,如果这些变量是对象,则仅将属性分配给变量才有意义。如果不是这样,那么分配将完全不做任何事情,甚至抛出错误。

So, as you can see, assigning properties to variables only makes sense if those variables are objects. If that is not the case, then the assignment will either do nothing at all, or even throw an error.

In在你的情况下,变量 test 包含 String 类型的值,所以这个:

In your case, the variable test contains a value of the type String, so this:

test.test = "test inner";

什么都不做。

但是,由于ES5引入了访问器属性,我上面说的有一个例外。访问器属性允许我们定义在检索或设置属性时调用的函数。

However, since ES5 introduced accessor properties, there is an exception to what I've said above. Accessor properties allow us to define functions which are invoked whenever the property is either retrieved or set.

例如:

var str = '';
str.prop;

这里 str 是一个持有<的变量em> String 值。因此,访问该变量的属性应该是no-op( str.prop 只返回 undefined )。这是正确的,但有一个例外:如果 String.prototype 包含一个带有定义的getter的访问者属性'prop',那么将调用getter。

Here str is a variable holding a String value. Therefore, accessing a property of that variable should be a no-op (str.prop merely returns undefined). This is true with one exception: if String.prototype contains a accessor property 'prop' with a defined getter, then that getter will be invoked.

因此,如果已定义:

Object.defineProperty( String.prototype, 'prop', {
    get: function () {
        // this function is the getter
    }
}); 

然后这个

str.prop;

将调用该getter函数。

will invoke that getter function.

现场演示: http://jsfiddle.net/fmNgu/

但是,我不认为在内置原型中添加访问器属性是一种很好的做法。

However, I don't think that adding accessor properties to the built-in prototypes would be a good practice.

这篇关于为什么我不能在javascript中为字符串对象添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆