在Javascript中替换属性键名称

时间:2015-10-14 01:31:23

标签: javascript object properties

我觉得这将是其中一个“哦,jeez,我怎么问这个问题”的问题,但是......

// So I create an object:
var o = {};

// I assign a string value to a variable:
var prop1 = "prop_key";

// And I use that variable that resolves to that string as the property name:
o[prop1] = "value1";

// => So far so good, the property key is the right value:
console.log(o); // => Object {prop_key: "value1"}

// Moving on, I am malicious and overwrite the `prop1` variable, replacing the original value to say, another string:
prop1 = "evil_string";

// And just to make sure it worked:
console.log(prop1); // => evil_value:

// AND YET, when I query the object...
console.log(o); // => Object {prop_key: "value1"}

现在不应该输出Object{"evil_value": "val1"},因为o[prop1]不再指向原来的prop_key值吗?

0 个答案:

没有答案