i am reading in a book that inheritance using the
Chef.prototype = new Person();
I want to know that why we use the instance of Person object for inheritance. I mean if i create a Chef object that will inherit all properties and method from a new Person Object. But why not inherit from Person Constructor? I am new to JavaScript so please bear with me, i am confused here. Thanks.
Ok so here is a code
var Person = function() {
this.bar = "bar";
};
Person.prototype.foo = "foo";
var Chef = function() {
this.goo = "goo";
};
Chef.prototype = new Person();
var cody = new Chef();
console.log(cody.bar);
How come Cody gets the properties of Person like bar when there was no instantiation of Person. i mean does the new Person()
runs in background every time i create a new Chef object?