ES3的Javascript getter/setter
我尝试在Photoshop中实现以下函数(使用Javascript ES3编写脚本)。我怎样才能将其编写为与ES3兼容ES3的Javascript getter/setter,javascript,photoshop-script,ecmascript-3,Javascript,Photoshop Script,Ecmascript 3,我尝试在Photoshop中实现以下函数(使用Javascript ES3编写脚本)。我怎样才能将其编写为与ES3兼容 function VParabola(s){ this.cEvent = null; this.parent = null; this._left = null; this._right = null; this.site = s; this.isLeaf = (this.site != null); } VParabola.p
function VParabola(s){
this.cEvent = null;
this.parent = null;
this._left = null;
this._right = null;
this.site = s;
this.isLeaf = (this.site != null);
}
VParabola.prototype = {
get left(){
return this._left;
},
get right(){
return this._right;
},
set left(p){
this._left = p;
p.parent = this;
},
set right(p){
this._right = p;
p.parent = this;
}
};
您可以在构造函数中使用
function VParabola(s){
this.cEvent = null;
this.parent = null;
var left = null;
var right = null;
this.site = s;
this.isLeaf = (this.site != null);
Object.defineProperty(this, 'right', {
get: function () {
return right;
},
set: function (value) {
right = value;
}
})
Object.defineProperty(this, 'left', {
get: function () {
return left;
},
set: function (value) {
left = value;
}
})
}
已定义的属性不应具有
\uu
前缀。回答得好。Object.defineProperty
是ES5,所以我怀疑它是否有效?这不起作用-Object.defineProperty
不是一个函数。您的环境是否支持Object.defineProperty
?或者Object.prototype.\uuu defineGetter\uuuu
?我相信Photoshop仅限于ES3-我得到Object.defineProperty不是一个函数
是的,另一个呢?这是一个非标准的ES3扩展。尝试编写对象。_udefinegetter__('right',function(){return right;})代码>在VParabola函数中。仍然不是一个函数。我不知道我写的是否正确。应该是VParabola.prototype.\uuu defineGetter\uuu('left',function(){返回这个。left;})代码>(当然在构造函数之外)