屬性的getter和setter聲明是否正確且最有效?爲JavaScript對象定義屬性
var AxisRange = (function(){
function AxisRange(){
this._userMaxValue = 0.0;
Object.defineProperty(AxisRange.prototype, "UserMaxValue", {
get : function(){
return this._userMaxValue;
},
set : function(value){
if(value != this._userMaxValue){
this._userMaxValue = value;
this.validateUserMaxValue();
this.validateUserStep();
this.synchronizeActualRange();
}
}
});
}
AxisRange.prototype.validateUserMaxValue = function(){
alert("validateUserMaxValue");
};
return AxisRange;
})();
而且,我使用JetBrains公司WebStorm寫我的JS代碼,並警告我說,
- 在
Object.defineProperty
使用AxisRange.prototype
是不能分配給參數類型的對象。 - in line
if(value != this._userMaxValue)
is said「可能的非法使用此。」
在繼續進行代碼輸入之前,我需要確保我使用的是正確的。
您應該將'Object.defineProperty'調用移到構造函數的外部,但除此之外它看起來很好。 –
http://www.w3schools.com/js/js_objects.asp幫助瞭解如何定義對象,屬性和方法 – SoluableNonagon
並且我同意Felix,getter/setter函數應該與validateUserMaxValue – SoluableNonagon