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:
- 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
- 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"
- 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?
- Why should I write Son.prototype.constructor=Son after inheritance?