However, I did not find any documentation that said in any way that it refers to a static method overridden by a child class, so the question is, can I always rely on this?
This is the standard function-call-through-object-property mechanism. When you do:
Child1.doImportantStuff();
... if doImportantStuff not an arrow function (this is not) or a related function (this is not), then during the call this set to Child1 . Similar:
var obj = { foo: function() { console.log(this === obj); } }; obj.foo();
So you can rely on that. (And I understand why you asked, it seems a little strange if you don't work it out.)
Of course, it will not work from non- static function code, because this will refer to the instance, and not to the constructor function. If you need it, you can use this.constructor.doImportantStuff if someone hasn't messed up the constructor property. (People are always used to messing it up, with the new syntax automating it, we hope this happens less, although you rarely need it ...)
For such questions, it is often useful to remember that the new class syntax is almost just syntactic sugar for the old verbose way we did it (if we were really solid). It is really good sugar, but that's almost all (and it's a good thing). static methods are set as properties of a constructor function, and not static methods are set as properties of an object in the prototype constructor property. (I think the only non-sugar aspect of this is that Bergi indicates that the new syntax allows us to extend built-in functions, such as Array , that werenโt possible to do this before. Some of the possible connection is related to how and when this set [you cannot access it until you call super() in your subclass constructor], which refers to new.target , which Bergi is discussing here . In ES7, it can go beyond sugar using confidential materials.)
Tj crowder
source share