The self keyword's ES2015 alternative

Posted on August 28, 2015 permalink comments

It’s quite a common use case that you need more types of constructors for your class, like copy constructors, or creating an instance from a plain object. With static methods, you can easily do this.

However, if you extend your class with these alternative “constructors”, you probably don’t want to redefine each in your child class just to use it’s own constructor, not the parent’s.

In this case, you need a keyword like PHP’s self, which refers to the class (or the constructor in ES5) of the instance.

Unfortunately ES2015 doesn’t have a keyword like this. However, due to the nature of Javascript, you can use the this keyword to access it. As the this keyword is not bound in static methods (thank you BabelJS@slack for confirming it), as long as call it as a method of the child class, this will refer to it.

Here’s an example of having a class which can be instantiated with a regular constructor or from a plain object:

Of course, if you just want to call the constructor, a new this() will do the trick in your static method.

An interesting bit is that even B.prototype.constructor.name is "B", the Chrome debugger still prints it as Object { ... }. After setting the constructor on the instance explicitly, the correct B { ... } form shows up. Apperently you don’t need to do this if you’re the using the new keyword.