Shubham Jain Shubham Jain - 4 months ago 27
JSON Question

Why cannot we call prototype functions?

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 !

Answer

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

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.

Comments