2015-01-16 116 views

回答

3

试试这个:

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; } 
} 
+0

非常感谢!也许你知道我遇到麻烦的原因? http://stackoverflow.com/questions/27986750/object-defineproperties-to-module-exports-in-nodejs –

相关问题