#2948 [JS] Private Field Accessors

SlimerDude Sat 6 Dec

If I have this class:

@Js class Meh {
  private Str? myField {
    get { null }
    set {      }
  }
	
  new make() {
    echo(myField)
  }
}

Then constructing it in JS results in:

sys::Err: $self.myField is not a function

For it seems the generated JS does not contain the accessor code:

class MyClass extends sys.Obj {
  constructor() {
    super();
    const this$ = this;
  }

  typeof() { return MyClass.type$; }

  #myField = null;

  // private field reflection only
  __myField(it) { if (it === undefined) return this.#myField; else this.#myField = it; }

  static make() {
    const $self = new MyClass();
    MyClass.make$($self);
    return $self;
  }

  static make$($self) {
    sys.ObjUtil.echo($self.myField());
    return;
  }
}

But if I remove the private scope:

@Js class Meh {
  Str? myField {
    get { null }
    set {      }
  }
  ...
}

Then the accessor code IS generated, and everything runs just fine.

class MyClass extends sys.Obj {
  ...
  
  #myField = null;

  myField(it) {
    if (it === undefined) {
      return null;
    }
    else {
      return;
    }
  }
  
  ...
}

Tested in Fantom 1.0.82 / SkySpark 3.1.12.

Login or Signup to reply.