Home  >  Article  >  Web Front-end  >  Interpretation of the relationship between constructors, prototypes and instances

Interpretation of the relationship between constructors, prototypes and instances

零下一度
零下一度Original
2017-06-26 11:53:071252browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn