Javascript getters/setters to ES3

909 views Asked by At

I have the following function which I'm trying to implement into Photoshop (uses Javascript ES3 for scripting). How could I write this to be ES3 compatible?

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

There are 1 answers

3
Shubham Khatri On

You can make use of Object.defineProperty in your constructor function like

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

 }