ES3的Javascript获取器/设置器

时间:2018-10-09 08:32:58

标签: 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.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;
    }
};

1 个答案:

答案 0 :(得分:1)

您可以在构造函数中使用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;
        }
    })

 }