简体   繁体   English

向javascript对象添加属性的不同方法

[英]Different methods of adding properties to javascript object

The following 3 methods i had come across by which properties can be added to an object. 我遇到了以下3种方法,可以通过这些方法将属性添加到对象。

> collection = {key0: 'value0'}
{ key0: 'value0' }

> collection.key1 = 'value1';
'value1'

> collection['key2'] = 'value2';
'value2'

> collection
{ key0: 'value0',
  key1: 'value1',
  key2: 'value2' }

Are there other methods exist by which properties can be added to the object. 是否存在其他可以将属性添加到对象的方法。

Does all the 3 methods do the exact same job ? 这三种方法是否都做完全相同的工作?

There is an important difference here: the first example creates a new object (this is called an "object literal"), the other two change an already existing object. 这里有一个重要的区别:第一个示例创建一个新对象(这称为“对象文字”),另外两个更改一个已经存在的对象。

The second and third examples change the collection object in the same way, the difference lies in the inability to use a variable property name in the dot notation: 第二个和第三个示例以相同的方式更改collection对象,不同之处在于无法使用点表示法中的变量属性名称:

// Let's say we have an object
var collection = { key0 : "value0" };

// Let's say for some reason we want to use a variable as a property name
var keyName = "key1";

// This won't assign "someValue" to the property "key1" as you would expect
// instead it will assign it to the property "keyName" (the string, not the variable)
collection.keyName = "someValue"; // OOPS

// With bracket notation you can use a variable (or any expression) as a property name
collection[keyName] = "value1"; // Sweet

The first statement defines a new object with a single property named key0 . 第一条语句定义了一个具有名为key0的单个属性的新对象。

The second statement assigns a value to the object's property named key1 . 第二条语句为对象的名为key1的属性分配一个值。 Since the object doesn't have its own property named key1 , the property is created. 由于对象没有自己的名为key1的属性,因此将创建该属性。

The third statement is identical in effect to the second statement. 第三条语句与第二条语句的作用相同。 The main reasons to use bracket notation instead of dot notation are: 使用括号表示法代替点表示法的主要原因是:

  • properties with special characters, eg, collection["foo-bar"] refers to a property called foo-bar , but collection.foo-bar performs a subtraction operation on collection.foo and bar . 具有特殊字符的属性,例如collection["foo-bar"]指的是称为foo-bar的属性,但是collection.foo-barcollection.foobar上执行减法运算。

  • variable property names, eg, you can do var propName = "key0"; collection[propName] = ... 可变属性名称,例如,可以执行var propName = "key0"; collection[propName] = ... var propName = "key0"; collection[propName] = ...

The only other way to define properties is with Object.defineProperty (and the multiple variant Object.defineProperties ). 定义属性的唯一其他方法是使用Object.defineProperty (以及多个变体Object.defineProperties )。 This allows you to define properties that behave in special ways. 这使您可以定义以特殊方式运行的属性。

Object.defineProperty(collection, "key3", {
    enumerable: false,    // property will not show up in for-in loops
    configurable: false,  // property cannot be changed
    set: function(val) {
        alert("we tried to set key3 to " + val);
    },
    get: function() {
        alert("this code runs when we get collection.key3");
        return "value3";
    }
});

collection.key3 = 6;  // alerts "we tried to set key3 to 6"
collection.key3;      // alerts "this code runs when we get collection.key3"
                      // and returns the string "value3"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM