我想在一個對象中設置兩個相等的屬性。這裏有一個例子:在聲明中設置兩個相等的屬性
var obj = { // I want to do something like this
a: function() { ... },
b: alert,
c: a
};
顯然不行,我必須做這樣的事情:
var obj = {
a: function() { ... },
b: alert,
};
obj.c = obj.a;
有沒有辦法做到這一點的聲明?
請參閱http://stackoverflow.com/questions/2787245/how-can-a-javascript-object-refer-to-values-in-itself,http://stackoverflow.com/questions/4618541/can- i-reference-other-properties-during-object-declaration-in-javascript,以及http://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations – 2011-05-12 00:43:07