2015-01-16 153 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 –

相關問題