BonsaiDen/JavaScript-Garden

About the inheritance

inomdzhon opened this issue · 3 comments

On chapter "The Prototype" for set instance used keyword "new", I mean Bar.prototype = new Foo();.

It's not good way, because we don't need create new object, futhermore it can take argument in the future. In particular we care only about prototype.

Best used Object.create(). For our example it's Bar.prototype = Object.create(Foo.prototype);. For IE8- we can used polyfill.

Thanks.

Sure - happy to take a PR.

Apparently there's no accepted PR yet, but a slightly more verbose way to have a prototype chain for instances of Bar that has no properties specific to Foo instances is to set up an empty function function F() {} with F.prototype = Foo.prototype;, and then set Bar.prototype = new F();; this is much of what the Object.create polyfill encapsulates, without the additional logic of dealing with null prototypes or adding own properties at object creation (the second parameter for Object.create).