Skip to content

Change value of static field from decorator (2018-09)? #15429

Answered by nicolo-ribaudo
joezappie asked this question in Q&A
Discussion options

You must be logged in to vote

This should work:

const addItem = (newValue) => {
  return function (target) {
    const { initializer } = target;
    target.initializer = function () {
      return [
        ...initializer.call(this),
        newValue,
      ];
    };
  };
};

class Test {
  @addItem(4)
  static values = [1,2,3];
}

const test = new Test();
console.log(test.constructor.values) // [1,2,3,4]

fields values are wrapped in an initializer function.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@joezappie
Comment options

Answer selected by joezappie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants