1

Looking at the answer to (How does the prototype chain work?) I can see that there is an inheritance chain. What is happening behind the scenes?

As far as I can tell the prototype property stores a reference to the prototype object? Why does that object not include the prototype's prototype and how is it maintaining that reference instead?

var Parent = function() {
	this.name = 'Parent';
}

Parent.prototype.sayHi = function() {
	console.log('hi');
}

var Child = function() {
	this.name = "Child";
}

Child.prototype = new Parent();

console.log(Parent.prototype); // { sayHi: [Function] }
console.log(Child.prototype); // { name: 'Parent' }
console.log(Child.prototype.prototype); // undefined

=============== Answer from below ===============

console.log(Parent.prototype); // { sayHi: [Function] }
console.log(Child.prototype); // { name: 'Parent' }
console.log(Child.prototype.__proto__); // { sayHi: [Function] }
3
  • 1
    That's not a correct output from your code. Create an inline snippet. Commented Jul 29, 2015 at 19:28
  • 2
    it does "include the prototype's prototype", if you setup such a relationship. in the code shown, there's no tying of Child to Parent, so it would be surprising if they were connected for you... Commented Jul 29, 2015 at 19:32
  • Sorry, now it does. But I still get that same output Commented Jul 29, 2015 at 19:38

1 Answer 1

3

Why does that object not include the prototype's prototype and how is it maintaining that reference instead?

Because in your example, the prototype of Child is also an instance of Parent, not another constructor. The prototype is a property of the constructor, not a property of each individual instance.

There is another property that does this for each instance, the __proto__ property, but it has almost no sane uses. The ES6 specification also only requires the feature be implemented in web browser, and not necessarily other JavaScript environments.

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

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.