-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.

4
  • 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. Commented Jan 18, 2015 at 4: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. Commented Jan 18, 2015 at 4: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 Commented Jan 18, 2015 at 4: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. Commented Jan 18, 2015 at 4:59

1 Answer 1

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.

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

4 Comments

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.
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.
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.