试试这个:
Object.defineProperties(window, {
propertyOne: {
get: function(){ },
set: function(i){ ... }
},
propertyTwo: {
get: function(){ },
set: function(i){ ... }
},
propertyThree: {
get: function(){ },
set: function(i){ ... }
}
});
等同于:
Object.defineProperty(window, "propertyOne", {
get: function(){ },
set: function(i){ ... }
});
Object.defineProperty(window, "propertyTwo", {
get: function(){ },
set: function(i){ ... }
});
Object.defineProperty(window, "propertyThree", {
get: function(){ },
set: function(i){ ... }
});
也有可能使用到对象文本分配功能时的get/set关键字(如设置一个JavaScript函数的原型时) :
var Example = function(element){
this.element = element;
};
Example.prototype = {
get colour(){ return this.element.style.backgroundColor; },
set colour(i){ this.element.style.backgroundColor = i; }
}
如果你做了很多,你可以写一个函数返回该函数。 –
这究竟是不是很简单? – Pointy