4 回答
TA贡献1828条经验 获得超3个赞
是。
var data = {
'PropertyA': 1,
'PropertyB': 2,
'PropertyC': 3
};
data["PropertyD"] = 4;
// dialog box with 4 in it
alert(data.PropertyD);
alert(data["PropertyD"]);
TA贡献1796条经验 获得超4个赞
var data = { 'PropertyA': 1, 'PropertyB': 2, 'PropertyC': 3};var propertyName = "someProperty";var propertyValue = "someValue";
data[propertyName] = propertyValue;
eval("data." + propertyName + " = '" + propertyValue + "'");
alert(data.someProperty);
data(data["someProperty"]);
alert(data[propertyName]);
TA贡献1818条经验 获得超8个赞
Object.defineProperty()
var o = {}; // Creates a new object
// Example of an object property added with defineProperty with a data property descriptor
Object.defineProperty(o, "a", {value : 37,
writable : true,
enumerable : true,
configurable : true});
// 'a' property exists in the o object and its value is 37
// Example of an object property added with defineProperty with an accessor property descriptor
var bValue;
Object.defineProperty(o, "b", {get : function(){ return bValue; },
set : function(newValue){ bValue = newValue; },
enumerable : true,
configurable : true});
o.b = 38;
// 'b' property exists in the o object and its value is 38
// The value of o.b is now always identical to bValue, unless o.b is redefined
// You cannot try to mix both :
Object.defineProperty(o, "conflict", { value: 0x9f91102,
get: function() { return 0xdeadbeef; } });
// throws a TypeError: value appears only in data descriptors, get appears only in accessor descriptors
添加回答
举报