0

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?

Cloudboy22
  • 1,496
  • 5
  • 21
  • 39
  • @Tushar i still don't get it.. i mean if i create a Chef object why cant it be linked to Person Object constructor i mean the constructor function and not the ptototype? – Cloudboy22 Dec 03 '15 at 06:18
  • That book seems to be [doing it wrong](https://stackoverflow.com/questions/12592913/what-is-the-reason-to-use-the-new-keyword-here) (and maybe it's severely outdated). You're challenging it with exactly the correct argument - it makes no sense to use a `Person` instance. Instead, you want your `Chef`s `.prototype` inherit from the `Person.prototype`, without having any instance properties. – Bergi Dec 03 '15 at 06:42
  • @Bergi really?? oh!! – Cloudboy22 Dec 03 '15 at 06:43
  • Yes, really. Which book is it? – Bergi Dec 03 '15 at 06:45

2 Answers2

1

The prototype itself is an object not a constructor that's why you assign your prototype to an object. The prototype is not just any method on the object it behaves a little differently. You may want to read this article particularly the function resolution section.

Edit: Now that the question is changed I will try to answer it.

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);

console.log(cody);

http://jsfiddle.net/4mghpr98/

If you run that in your browser and check your console it may make more sense what is happening.

chef

So cody (your Chef instance) has a property __proto__ that is your object instance of person you created and assigned to Chef.prototype. All instances of chefs will have the object prototype that you assigned to Chef.prototype. The __proto__ is special because when you access the bar variable if it is not found on the main chef instance it will check up the prototype chain until it finds it. I say prototype chain because, your prototypes can even have multiple prototypes, but it's a chain because their is only a single parent relationship for each one.

Make sense? It's all explained in that article above in the exact section I talked about.

John
  • 7,114
  • 2
  • 37
  • 57
  • 1
    "*your prototypes can even have multiple prototypes*" - no, every object has only a single prototype from which it inherits. Multiple objects may form a chain of inheritance of course. – Bergi Dec 03 '15 at 06:43
  • Yeah @bergi I know I updated that to reflect that. That was not my original intention, and that statement is not completley incorrect but by itself it is misleading. – John Dec 03 '15 at 06:45
-1

Well .prototype is something that you can consider the root. Every object you make roots to something called the Object.prototype if you make changes in the Object.prototype changes will be reflected in all the objects.

Similarly, if you make some object with the name "chef" all objects inherited from this object (chef) will be 1st rooted to chef.prototype and then it will root to object.prototype.

Hrishikesh
  • 335
  • 1
  • 13