Conversation
| } | ||
|
|
||
| @bind nextMe = () => { | ||
| return this.prop; |
There was a problem hiding this comment.
If you put a debugger here, the context of this is the getter {enumerable: true, configurable: false, writable: true, initializer: ƒ}
|
So Given this class: class Foo {
bar = 'baz';
}It transpiles to something like this: const FooInitializers = {
bar() {
return 'baz';
}
}
class Foo {
constructor() {
for (let initializer in FooInitializers) {
let initializerFunc = FooInitializers[initializer];
this[initializer] = initializerFunc.apply(this);
}
}
}This may seem odd, but it's actually much better for JS classes to always initialize values like class fields in the constructor. For one thing, new objects and arrays will always be created, so we don't shared object or array instances between classes. For let descriptor = {
writable: true,
configurable: true,
enumerable: true,
initializer() {
return run.bind(this, method);
}
}Where method is the extracted value of the original descriptor. The |

So leaving this here for discussion.
I came across a few issues relating to the context of
thisin an initializer function. I'm still digging in...I did try as a method property as well. As a prototype property, the decorator function gets the prototype passed as an argument. So swapping out the desc.value with
initializerdoesn't affect the call order.tc39/proposal-class-public-fields#34
babel/babel#6977
tc39/proposal-class-fields#62