Confused about JavaScript prototypal inheritance?

Let say, Dog is a Mammal. Function Mammal() { this. Milk = true; }; function Dog() { this.

Bark = true; } Dog. Prototype = new Mammal; So prototype of Dog points to an object of Mammal. This Mammal object has a reference to its constructor so when Dog is new, JavaScript see that Dog prototype is a Mammal so Mammal's constructor is called to produce a valid Mammal object (another one) then make it a Dog object using Dog constructor.

From this, the constructor of Dog. Prototype is a Mammal (a Mammal Object that has extra fields and functions added) BUT constructor of Dog is Dog. The inheritance exist because the an instance of Dog has a Mammal as a prototype; hence, Dog is a Mammal.

When a method is called and JS cannot find it from Dog. Prototype, JS look in Mammal. Prototype (which is an Object that has extra fields and functions added).

Hope this helps.

Unfortunately, based on the code you've written, var d = new Dog(); if(d. Constructor === Dog) console. Log('yes!') else console.

Log('oh noes! ') will log oh noes!. This is because you have overwritten Dog.

Prototype with a Mammal object, so d. Constructor will point back to Mammal. A common pitfall that makes .

Constructor unreliable. – Sean McMillan Jun 28 '11 at 18:07.

Don't worry about the constructor property - it is irrelevant. Skip those sentences and you might follow it better. If you are still unsure of your understanding, google for __proto__ - the internal prototype reference on JS objects.It is even exposed to scripts on Firefox and Safari.

A good reference is https://developer.mozilla. Org/en/Core_JavaScript_1.5_Guide/The_Employee_Example/Object_Properties/Inheriting_Properties.

If you have an object obj, it's prototype is obj. Prototype and constructor property referring to obj's constructor is obj.prototype.constructor. For the object obj.

Prototype the situation is the same. Let's say proto = obj. Prototype, then the reference to the constructor of proto would be found at proto.prototype.constructor.

This is the same as obj.prototype.prototype. Constructor, so there is no conflict with obj.prototype.constructor.

Obj. Prototype is not to prototype of obj. If obj = new XYZ then XYZ.

Prototype is the prototype of obj. – Sean McMillan Apr 5 '11 at 1:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions