1

Initially I assigned function object to variable Person. At this moment, Person's proto is pointing to Function.prototype. Then I added some functions to Person.prototype. When I call Person constructor with new keyword below and assign it to var test, as far as I know, it sets test proto to Person.prototype. This Person.prototype is an object, with getName mapped to a function. So when I call test.getName(), it searches for the function in test.prototype and if it doesn't find it there, then it will search for the function in its proto i.e. Person.prototype.

Now suppose I create another function object and assign it to variable Customer. It's proto would be pointing to Function.prototype. Then for inheritance we should do Customer.prototype = new Person(). I am confused about why I should do this? Does it sets Customer's proto to Person.prototype. If it does , then shouldn't I just write Customer = new Person(). If it doesn't than, do Customer proto still points to Function.prototype or is there something else that I am missing ?

var Person = function(name) {
    this.name = name;
    console.log("Running the constructor of Person " + name)
}

Person.prototype.getName = function() {
    return this.name;
}

var test = new Person("Yolo");
console.log(Person.prototype.getName.call(test))
console.log(test.getName());

var Customer = function(name) {
    this.name = name;
    console.log("Running the constructor of Customer " + name)
};

Customer.prototype = new Person();

Thanks in advance !

1
  • Hint: What's the difference between var test = new Person() and var Test.prototype = new Person() Commented Jul 17, 2016 at 11:04

3 Answers 3

1

There are two different concepts here:

  • __proto__ is the object's parent in the inheritance tree.
  • prototype is a function's property which defines what the __proto__ will be of any objects it creates when used as a constructor.

Since constructors are objects themselves (function objects), they also are in an inheritance tree, but that tree has nothing to do with the inheritance tree of the objects created by these constructors. They are two separate worlds.

Look at this code, which illustrates the inheritance chain for a customer object:

var cust = new Customer('Jack');
console.log(cust.__proto__ === Customer.prototype); // true
console.log(cust.__proto__.__proto__ === Person.prototype); // true
console.log(cust.__proto__.__proto__.__proto__ === Object.prototype); // true

So here we followed the prototype chain of the cust object. This is an entirely different chain from the chains for the constructors (functions):

console.log(Customer.__proto__ === Function.prototype); // true
console.log(Person.__proto__ === Function.prototype); // true
console.log(Object.__proto__ === Function.prototype); // true

Nothing unusual here: they are functions. Also, it is not necessary to change anything to that. As said, it does not relate to the inheritance for the objects these constructors create.

Note that you should never need to access the __proto__ property. The above is only provided for illustrating the inheritance chain.

Sign up to request clarification or add additional context in comments.

1 Comment

Finally the enlightenment moment. Thanks a lot.
1
Customer.___proto____ = Function.prototype 

It will always be pointing to the Function prototype

Customer.prototype = new Person();

This statement will make Customer.prototype._____proto_____ point towards Person.Prototype

Customer._____proto_____ will never change only its prototype 's ____proto___ changes by inheritance

3 Comments

This is an implementation detail that's not part of the javascript standard. Always use .prototype
This is how inheritance works .I am just explaining how proto links are setup.Obviously these proto links are not used in code
But then when I do like this, var test2 = new Customer(); and call test2.getName(), then it will search for the function in test2.prototype, then test2 proto prototype, i.e. Customer.prototype, it wouldn't find it there, so it will search it in Customer's proto i.e. Function.prototype. So, this way it never reaches real function.
0

enter image description here

I found this image useful. It shows all the links clearly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.