本文介绍了__defineGetter__的整洁替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
吸烟者和二传手是VB.Net的美女:
Getters and setters are a beauty in VB.Net:
Get
Return width
End Get
Set(ByVal value As Integer)
width = value
End Set
在Javascript中,这可能是我们要做的:
In Javascript, this is probably what we would do:
function Test() {
var width = 100;
this.__defineGetter__("Width", function() {
return width;
});
this.__defineSetter__("Width", function(value){
width = value;
});
}
看起来像是一盘被kuri洗过的意大利面。我们有哪些更简洁的选择?
It looks like a plate of spaghetti ransacked by a kuri. What are some neater alternatives we have?
推荐答案
使用ES5,您将能够:
With ES5 you'll be able to do:
function Test() {
var a = 1;
return {
get A() { return a; },
set A(v) { a = v; }
};
}
getter / setter函数当然可以做任何你想要的事情。
The getter/setter functions can of course do anything you want them to.
这篇关于__defineGetter__的整洁替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!