-1

So my question is really in code. Please refer to the following section:

function Person(){};
function Ninja(){};

// Creating ninja object
var ninja = new Ninja();
console.log("ninja constructor with empty prototype object = " +
    ninja.constructor);

// Attaching new prototype to Ninja
Ninja.prototype = new Person();
console.log("ninja constructor with prototype(Person) = " +
    ninja.constructor);

// The previous console.log printed out the correct value. Now for the
// confusing part! For every other new Ninja object the constructor is no more
// Ninja
var ninja1 = new Ninja();
console.log("ninja1 constructor with prototype(Person)= " +
    ninja1.constructor);

var anotherNinja = new Ninja();
console.log("anotherNinja constructor with prototype(Person)= " +
    anotherNinja.constructor);

console.log("Again ninja constructor with prototype(Person)= " +
    ninja.constructor);

How is the constructor of the new objects pointing to Person? They were originally created from Ninja. This happened only after I changed the prototype of Ninja.

However, the previous Ninja object I created before changing the prototype still holds true its constructor still points to Ninja even though the prototype was changed.

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
Lakmal Caldera
  • 1,001
  • 2
  • 12
  • 25
  • 2
    _“However, the previous Ninja object I created before changing the prototype still holds true its constructor still points to Ninja”_ – of course it does, because that was the constructor used when that object was created. – CBroe Jan 18 '15 at 04:46
  • Well of course there is nothing wrong with that. The question is in the previous line. How is the constructor of the new objects pointing to Person? They were not created from Person, but from Ninja. – Lakmal Caldera Jan 18 '15 at 04:49
  • _“They were not created from Person, but from Ninja”_ – but you explicitly made `Person` the function that is used when a new Ninja is created _by overwriting Ninja’s `prototype` …_ – CBroe Jan 18 '15 at 04:53
  • Im sorry but how is Person being called when I create a new Ninja object just because I changed the prototype? The direct constructor still used to create new objects is Ninja right? not Person. Person is just an attached prototype. I did not explicitly change the constructor anywhere. – Lakmal Caldera Jan 18 '15 at 04:59

1 Answers1

0

The constructor property returns a reference to the object that created the instance's prototype. It's not a string value containing the name of the function that created it.

Mike Loffland
  • 344
  • 1
  • 10
  • If i may add to that.. the constructor property actually returns the second object in the pototype chain from the top. The top most object being Object. From some reason it doesn't return Object(first object in the chain) but the second object in the chain. – Lakmal Caldera Jan 18 '15 at 06:18
  • The reference is always to the prototype... That's why it's called the "prototype chain" and not the "constructor chain". It's not that it has a "reference to the second object in the chain"... It's that the given object always has a reference to the previous object in the chain. – Mike Loffland Jan 18 '15 at 06:44
  • Think of it like this... When you say var ninja1 = new Ninja(); (after you've altered Ninja's prototype). JavaScript first uses Person() to create a new Ninja (inheriting all of Person's properties and methods)... So, the "constructor" (as far as JavaScript is concerned) is Person. – Mike Loffland Jan 18 '15 at 07:00
  • Yet another way to think about it is that the constructor property is that it will always point to the last object that used 'new' operator in the given object's prototype chain. I wouldn't worry about the constructor property as it relates to inheritance In JavaScript... Always look to the prototype. Object.getPrototypeOf will always give you the "real" answer. – Mike Loffland Jan 18 '15 at 07:18