Home > Article > Web Front-end > Interpretation of the relationship between constructors, prototypes and instances
---Restore content begins---
Each constructor has a prototype object. The prototype object contains a pointer to the constructor, and the instance contains an internal pointer to the prototype object. , realize inheritance through the prototype chain
The following code example
function Parent(){
this.hobby = 'play';
};
Parent.prototype.showHobby = function(){
Return this.hobby;
};
function Son(){
This.hobby = 'eat';
};
//Realize inheritance and inherit hobby;
Son.prototype = new Parent();
son.Prototype.showSonhobby = function(){
Return this.Sonhobby;
};
var obj = new Son();
alert(obj.showHobby());
for(var i in obj){
document.write(i + '---' + obj[i] + '< ;br/>');
};
---End of recovery content---
The above is the detailed content of Interpretation of the relationship between constructors, prototypes and instances. For more information, please follow other related articles on the PHP Chinese website!