4 ms·
It is not legal JS as of January 2019. > Why would I put this.y = 10 in the constructor? Because that's how you initialize attributes of an object instance in
by randallsquared 8y ago
It is not legal JS as of January 2019.
> Why would I put this.y = 10 in the constructor?
Because that's how you initialize attributes of an object instance in JS, in January 2019.
I don't believe this code does what you think it does. Even if your definition of `x` generated an attribute of instances of C, using an arrow function would defeat your apparent intention: arrow functions take their surrounding lexical scope's `this`, which in this case is the class, not any instance you define from the class.
> If you don't bind `this` to `method` in the constructor, when you call `instance.method()`, `this` will be whatever (window, IIRC).
Simply not the case, as another commenter has pointed out: `instance` in your loop is definitely `method`'s `this`, and not `window`.
- dmitriid 8y ago> It is not legal JS as of January 2019. Ah, true. I keep forgetting class fields are not standardized yet since eve ryone is using them anyway just because they are so damn handy. > Even if your definition of `x` generated an attribute of instances of C, using an arrow function would defeat your apparent intention: arrow functions take their surrounding lexical scope's `this`, which in this case is the class, not any instance you define from the class. For the current implementations of class fields this works. If they change that behaviour, what good are classes?
- randallsquared 8y ago> If they change [arrow functions taking `this` from lexical scope instead of caller], what good are classes? Using an arrow function on a class field seems like a way to get static methods that are still callable from the instance. But I'm not sure I have a burning need for that at the moment...