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