Saturday, 25 July 2015

Set [[Prototype]]

When years ago I first heard about the plans to add classes to ES6 I was terrified they could intend to mutate the language from its Prototype based nature to a standard Class based paradigm. Almost immediately I found comments explaining that the addition of classes would be just syntactic sugar, and these classes would be translated by the compiler into the prototype chains we are used to.

Now that ES6 as a standard is a reality, and hopefully most ES engines will be implementing it pretty soon, it feels like it's time to get ready for it. On one side you can read here how indeed classes are just syntactic sugar.

ES6 classes are a simple sugar over the prototype-based OO pattern.

But furthermore you can read here that a standard way has been added to set the [[Prototype]] (aka internal Prototype) of an Object, by means of the Object.setPrototypeOf(obj, proto) method. We will no longer need to use the non standard __proto__ property, neither for setting nor for getting (as since ES5 we've had Object.getPrototypeOf(obj)).

It's noteworthy though the warning in the MDN about how slow setPrototypeOf can be.

1 comment: