Getter 和 Setter 是 VB.Net 中的一个美妙之处:
Get
Return width
End Get
Set(ByVal value As Integer)
width = value
End Set
在 Javascript 中,我们可能会这样做:
function Test() {
var width = 100;
this.__defineGetter__("Width", function() {
return width;
});
this.__defineSetter__("Width", function(value){
width = value;
});
}
它看起来就像是一盘被库里人洗劫一空的意大利面。我们有哪些更简洁的选择?
Note: The new code should access the value using new Test().Width
and not new Test().Width()
.
使用 ES5,您将能够执行以下操作:
function Test() {
var a = 1;
return {
get A() { return a; },
set A(v) { a = v; }
};
}
getter/setter 函数当然可以执行您希望它们执行的任何操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)