-2

i have the below code snippet

function Dad(){

  this.name="i am dad";
}

function Son(){
   this.nameOfChild="child";   
}

var sonBeforeInheritance= new Son();
alert(sonBeforeInheritance.constructor);  //outputs Son class

//inheritance done
Son.prototype= new Dad();

var sonObj= new Son();
alert(sonObj.constructor); //outputs Dad class    
alert(sonBeforeInheritance.constructor);  //outputs Son class

my undestanding please answer with true/false and explanation:

  1. although functions in js are also objects but the objects which are created with new keyword and prototype object are the only ones which have access to the property called constructor i.e. sonObj.constructor and Son.prototype.constructor is valid
  2. function in js have an object called prototype which objects defined with new keyword dont have i.e. sonObj above doesn't have prototype i.e. sonObj.prototype is "undefined"
  3. now constructor points to the class which was used to make the object? if true then why does sonObj.constructor gives me dad class? one might say that prototype of Son has changed then is the Son.prototype.constructor and "instance of Son".constructor point to same object?
  4. Why should I write Son.prototype.constructor=Son after inheritance?
Rishul Matta
  • 3,383
  • 5
  • 23
  • 29
  • 1
    Have you read the **Related** questions in the sidebar? In particular, it looks like http://stackoverflow.com/questions/541204/prototype-and-constructor-object-properties?rq=1 should address most of your questions. – Barmar Jul 12 '14 at 08:08
  • i wanted clarification on what happens before and after inheritance thats why i posted it from the explanation below i could understand that every obj created with new ref to constructor obj which is class.prototype.constructor but if class.prototype.constructor is overwritten then obj created before inheritance will continue pointing to the old one which is not talked about in the link you have given @Barmar – Rishul Matta Jul 12 '14 at 08:42
  • You should not create an instance of Parent to set the prototype part of inheritance for Child use `Son.prototype = Object.create(Dad.prototype)` instead. To inherit instance specific members or re use Parent constructor code in Child call the Parent constructor: `Parent.call(this,arguments);` Re assigning prototype does not affect already created instances but mutating the prototype does. More information about constructor functions and prototype here: http://stackoverflow.com/a/16063711/1641941 – HMR Jul 12 '14 at 12:49

1 Answers1

1

although functions in js are also objects but the objects which are created with new keyword and prototype object are the only ones which have access to the property called constructor

No. Most objects in js do have a .constructor property, they inherit it from their prototype. Also plain objects created by {} inherit fro Object.prototype and their .constructor will point to Object, just as arrays inherit .constructor == Array from the Array.prototype or functions inherit .constructor == Function from Function.prototype. They don't need to be created by new necessarily.

function in js have an object called prototype which objects defined with new keyword dont have i.e. sonObj above doesn't have prototype i.e. sonObj.prototype is "undefined"

True. That's how .prototype works.

now constructor points to the class which was used to make the object?

Not necessarily. .constructor is an ordinary property on the prototype so that it will be inherited by the instances, and is initialized to be the function to which the prototype object belongs. Both the .prototype and .constructor are overwritable, though.

if true then why does sonObj.constructor gives me dad class?

Because you've overwritten Son.prototype with an object whose .constructor property is not Son, but the inherited value Dad (you've changed it to be a new Dad instance - questionable, btw)

one might say that prototype of Son has changed then is the Son.prototype.constructor and "instance of Son".constructor point to same object?

Yes.

Why should I write Son.prototype.constructor=Son after inheritance?

To fix it and make it work like you initially expected. See also What is the `constructor` property really used for?.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375