4
function Human(){
  this.job = 'code'
}

//Human.prototype = {feeds: 'Pizza'};

var developer = new Human();

console.log(developer.constructor);

Above console logs

function Human() {
this.job = 'code';
}

When i uncomment the line Human.prototype = {feeds: 'Pizza'}; it console logs

function Object() {
  [native code]
}

Why setting prototype on constructor function, affects who is the constructor on object created by the constructor?

Another example:

function LivingBeing() {
  breathes: 'air';
}

function Human(){
  feeds: 'Pizza';
}

//Human.prototype = new LivingBeing();

var developer = new Human();
console.log(developer.constructor);

With commented like it says constructor is Human, when uncommented it says LivingBeing. Why constructor traverses further when something valid found on prototype?

I thought to add one more level to this

function AThing(){
  this.say = function(){return 'I am thing';};
}

function LivingBeing() {
  breathes: 'air';
}

LivingBeing.prototype = new AThing();

function Human(){
  feeds: 'Pizza';
}

Human.prototype = new LivingBeing();

var developer = new Human();
console.log(developer.constructor);

Now its says developer's constructor is AThing. Can i say constructor goes as far as possible in the prototype chain?

2
  • exact duplicate of Why is the constructor changed? Commented Apr 18, 2014 at 10:22
  • Sorry, I couldn't spot that while i am posting. Commented Apr 18, 2014 at 10:35

2 Answers 2

2

developer has no own property named constructor therefore when you're asking for it, it's looked for in the prototype chain. Since the prototype is a plain Object, its constructor is the default object constructor Object().

When you declare a new function like function Human(), JS creates an implicit object and populates prototype and constructor fields as follows:

X = {}
Human.prototype = X
X.constructor = Human

now, when you write dev = new Human, the internal __proto__ property of dev is set to X and dev.constructor resolves to X.constructor, which is Human.

When you uncomment the "pizza" line, the code turns into the following:

Human.prototype = X
X.constructor = Human

pizza = {feeds:'Pizza'}
// pizza.__proto__ = {}
// pizza.constructor = pizza.__proto__.constructor = Object

Human.prototype = pizza
// note that pizza.constructor does NOT change

dev = new Human
// dev.__proto__ = Human.prototype = pizza

dev.constructor
// dev.constructor = dev.__proto__.constructor = pizza.constructor = pizza.__proto__.constructor = Object()
Sign up to request clarification or add additional context in comments.

Comments

0

What is a constructor?

Returns a reference to the Object function that created the instance's prototype. Note that the value of this property is a reference to the function itself, not a string containing the function's name. The value is only read-only for primitive values such as 1, true and "test".

First Case:

The constructor of developer was the function Human(){this.job = "code";}.

Second Case:

Since you are overriding the prototype to object, now the developer is an instance of Object {}.

The Console shows any native global objects, predefined in the implementationof javascript itself as [native code]. That is they don't reveal the implementation.

3 Comments

Yes, but developer is still created with the same Human constructor.
Yes, but when prototype of Human is changed, the object itself is changed.
You mean the function object

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.