Javascript геттеры / сеттеры для ES3 - PullRequest
0 голосов
/ 09 октября 2018

У меня есть следующая функция, которую я пытаюсь реализовать в 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 голосов
/ 09 октября 2018

Вы можете использовать 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;
        }
    })

 }
...