Start a new topic

Object.defineProperty TYPE support for POCO getter / setter

Is there any way to "DECORATE" a POCO backing field property as a property

in JavaScript.


If i make a class:


private string _username;
public string username {
get { return _username; }
set { SetProperty (ref _username, value); }
}


in javascript... it has to be used as get_username() instead of just username.


I think the javascript Object.defineProperty allows you to tell a javascript object prototype to wrap the getter/setter... example:


Object.defineProperty(Aaa.prototype, "y", {
        get: function () {
          return this._y;
        },
        set: function (value) {
          this._y = value;
        },
        enumerable: true,
        configurable: true
      });
      return Aaa;

Will allow Aaa.y in javascript


Maybe some kind of JS attribute so our POCO backing field properties can be used by the simple name like you can in C#.



Sorry... I think i found the JS Attribute just enable getter/setter as properties: Just mark the public

getter/setter with JS.DefineProperty attribute... In javascript will see the backing field as _person BUT now also create get person and set person on the underlying javascript object.


private Person _person;
[Js (DefineProperty = "person")]
public Person person {
get { return _person; }
set { SetProperty (ref _person, value); }
}


You are right, please use:

[Js (DefineProperty = "person")]


Tip, you can also specify

[Js (DefineProperty = "")]

it will take the property name from C#, good for refactoring!

Login or Signup to post a comment