You change the Apple.prototype link after , you created an instance of a .
Changing the link here does not change it for existing instances.
You will also find
var a = new Apple(); Apple.prototype = {};
i.e. because we changed the inheritance chain of Apple a no longer considered Apple.
Setting Foo.prototype = null will raise a TypeError if you try to do instanceof Foo validation
Changing the property of an object does not change the reference to this object. eg
var foo = {}, bar = foo; foo.hello = 'world'; foo === bar;
Changing the object itself changes the link
foo = {hello: 'world'}; foo === bar; // false
Or itβs described in more detail how the prototype refers to an instance,
var Foo = {}, // pseudo constructor bar = {}, baz = {}; var fizz = {}; // fizz will be our pseudo instance Foo.bar = bar; // pseudo prototype fizz.inherit = foo.bar; // pseudo inheritance Foo.bar = baz; // pseudo new prototype fizz.inherit === foo.bar; // false, instance inheritance points elsewhere
The best practice for setting up the inheritance chain is not using new , but using Object.create
Apple.prototype = Object.create(Fruit.prototype);
If you need the Fruit constructor called by Apple instances, you would do
function Apple() {
source share