gpt4 book ai didi

ES3 的 Javascript getter/setter

转载 作者:行者123 更新时间:2023-11-30 20:07:40 24 4
gpt4 key购买 nike

我有以下功能,我正在尝试将其实现到 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.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;
}
};

最佳答案

您可以使用 Object.defineProperty在你的构造函数中,比如

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;
}
})

}

关于ES3 的 Javascript getter/setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52716612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com