0
说我这样做:在JavaScript中将对象的名称作为字符串传递时,我可以访问对象吗?
Foo = function() {};
o = Object.create(Foo, { p: {
value: 42,
writable: true,
enumerable: true,
configurable: true }
});
o.prototype.bar = function() {console.log("hello");};
var x = "o";
window.o = o;
Object.defineProperty(window[x], "foo", { value: 123});
问题:
有什么办法来设置o
一个属性,而不首先将其添加到另一个对象(在这种情况下,窗口)?
好主意与邪恶'eval' – frequent
或坏主意......取决于你正在使用它。 – Bergi
在我的情况坏主意...仍然 - 没有考虑它。 – frequent