I discovered that ES2015 classes prevent setting (redefining) their prototype.
It is often claimed that ES2015 classes are just "syntactic sugar" on top of ES5 constructor functions and prototype based inheritance.
But this is a difference in behavior...
Is this behavior part of the ES2015 specification? I did not find any documentation about this ...
The following examples illustrate the difference:
function Pet() {}
Pet.prototype.eat = () => {
console.log('Pet is eating ...');
}
Pet.prototype = {eat: function(){console.log('Pet is REALLY eating ...')}};
const pet = new Pet();
pet.eat(); // -> Pet is REALLY eating ...
console.log(Object.getOwnPropertyDescriptor(Pet, 'prototype'));
=> Redefining prototype of Pet works
class Pet {
eat() {
console.log('Pet is eating ...');
}
}
Pet.prototype = {eat: function(){console.log('Pet is REALLY eating ...')}};
const pet = new Pet();
pet.eat(); // -> Pet is eating ...
console.log(Object.getOwnPropertyDescriptor(Pet, 'prototype'));
=> Redefining prototype of Pet does not work
Any pointers to documentation of this behavior would be appreciated ...