Home > Mobile >  Value assignment is faling
Value assignment is faling

Time:03-16

I'm working with some object-oriented google app scripts. I'm creating my first test object and some of the passed arguments are being assigned to the internal variables, and some are not. All of the arguments are strings. Here's a clip of my constructor method and the logging output.

      constructor(desc, start, end){
        this.desc = desc;
    Logger.log('object start: %s, %s', start, this.start);    
        this.start = start;
    Logger.log('object start: %s, %s', start, this.start);    
        this.end = end;

I feel like there's something staring me in the face that I'm missing, but how can the assignment of the argument to the internal variable simply not happen? Logger output:

2:20:55 PM  Info    object start: 20220017, null
2:20:55 PM  Info    object start: 20220017, null

CodePudding user response:

Description

Here is a simple test of an object constructor in Google App Script

Script

function dummy() {
  try {
    var obj = new TestObject(1,2,3);
    console.log("obj.var1 = " obj.getVar1);
    console.log("obj.var3 = " obj.getVar3);
    obj.setVar3 = 10;
    console.log("obj.var3 = " obj.getVar3);
  }
  catch(err) {
    console.log(err)
  }
}

class TestObject {
  constructor(var1,var2,var3) {
    console.log("this.var1 = " this.var1)
    this.var1 = var1;
    console.log("this.var1 = " this.var1)
    this.var2 = var2;
    this.var3 = var3;
  }
  get getVar1() { return this.var1; }
  get getVar2() { return this.var2; }
  get getVar3() { return this.var3; }
  set setVar1(var1) { this.var1 = var1; }
  set setVar2(var2) { this.var2 = var2; }
  set setVar3(var3) { this.var3 = var3; }
}

Console.log

7:28:34 PM  Notice  Execution started
7:28:36 PM  Info    this.var1 = undefined
7:28:36 PM  Info    this.var1 = 1
7:28:36 PM  Info    obj.var1 = 1
7:28:36 PM  Info    obj.var3 = 3
7:28:36 PM  Info    obj.var3 = 10
7:28:34 PM  Notice  Execution completed

Reference

CodePudding user response:

I've discovered the ultimate cause of the problem.

There is a limitation when created classes: the arguments passed to the constructor cannot have an associated setter method. As soon as I commented the setter method out, the initialization of the internal variable worked as expected. This is not a limitation that I came across when reading up on javascript-specific object-oriented programming.

  • Related