1

I am working on GAS(Google Apps Script) where I use ES5 javascript.

I defined a class as follows:

var Test = function(a){
  this._a = a;
}

Test.prototype.doSomething = function(){
  //do something here
}

And then, I want to add setter and getter for this._a in the same way as Test.prototype.doSomething is defined.

I found this code to define setter and getter in this link:

var o = {
    a: 7,
    get b() {
        return this.a + 1;
    },
    set c(x) {
        this.a = x / 2
    }
};

However, I don't know how to apply this way to my case.

Could you please tell me how? Thanks in advance.

Herbert
  • 540
  • 1
  • 6
  • 18

1 Answers1

3

Google Apps Script is an interesting dialect of JavaScript in that it is both very out of date (though I understand that's going to get fixed) and yet has the odd smattering of modern things in it.

There are a couple of older ways to do what you're trying to do, hopefully one of them works in GAS:

Object.defineProperty

This is still modern, but was added long enough ago GAS may have it: Using Object.defineProperty, like this:

var Test = function(a){
    this._a = a;
};

Test.prototype.doSomething = function(){
    //do something here
};

Object.defineProperty(Test.prototype, "a", {
    get: function() {
        return this._a;
    },
    set: function(value) {
        this._a = value;
    },
    configurable: true
});

Using get and set Syntax

...to define a getter and a setter, as in your example in the question.

var Test = function(a){
    this._a = a;
};

Test.prototype = {
    constructor: Test,
    doSomething: function(){
      //do something here
    },
    get a() {
        return this._a;
    },
    set a(value) {
        this._a = a;
    }
};

Note that when you completely replace the object on the prototype property like that, you want to be sure to define the constructor property as shown above (it's defined that way by default in the object you get automatically, but if you replace it, naturally it's not automatically provided on the replacement).

The REALLY Old Way

I doubt GAS supports it, but just in case, the really old way to do it uses __defineGetter__ and __defineSetter__ like this:

var Test = function(a){
    this._a = a;
};

Test.prototype.doSomething = function(){
    //do something here
};

Test.prototype.__defineGetter__("a", function() {
    return this._a;
});
Test.prototype.__defineSetter__("a", function(value) {
    this._a = value;
});

This was never officially part of ECMAScript, but it was in the dialect of JavaScript from Mozilla for years (and probably still is, for backward compatibility).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Hi.. GAS was updated to v8 a month or two ago. See [tag info page](https://stackoverflow.com/tags/google-apps-script/info) for more details. However op might still be using old engine. So, I think this is useful – TheMaster Apr 27 '20 at 16:42
  • Yes, I know "Using get and set Syntax" way but as you explained, I don't want it to happen that it completely replace the object on the prototype property. So, I apply "Object.defineProperty" way. Thank you so much! – Herbert Apr 27 '20 at 16:45
  • @TheMaster Thanks for you advice. Yes, I know v8 update. But It has some issue so I have postponed using v8 because of this issue : https://issuetracker.google.com/issues/149413841 – Herbert Apr 27 '20 at 16:49
  • @TheMaster - Thanks! I'd heard that was in the works, I'm glad to hear it's gone ahead! – T.J. Crowder Apr 27 '20 at 16:51
  • @Herbert - `defineProperty` worked? That's good to know. I was about 75% on whether it would... :-) – T.J. Crowder Apr 27 '20 at 16:52