From the MDN docs for the standard setPrototypeOf
function as well as the non-standard __proto__
property:
Mutating the [[Prototype]] of an object, no matter how this is accomplished, is strongly discouraged, because it is very slow and unavoidably slows down subsequent execution in modern JavaScript implementations.
Using Function.prototype
to add properties is the way to add member functions to javascript classes. Then as the following shows:
function Foo(){}
function bar(){}
var foo = new Foo();
// This is bad:
//foo.__proto__.bar = bar;
// But this is okay
Foo.prototype.bar = bar;
// Both cause this to be true:
console.log(foo.__proto__.bar == bar); // true
Why is foo.__proto__.bar = bar;
bad? If its bad isn't Foo.prototype.bar = bar;
just as bad?
Then why this warning: it is very slow and unavoidably slows down subsequent execution in modern JavaScript implementations. Surely Foo.prototype.bar = bar;
is not that bad.
Update Perhaps by mutation they meant reassignment. See accepted answer.