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

Interpretation of the relationship between constructors, prototypes and instances

零下一度
Release: 2017-06-26 11:53:07
Original
1282 people have browsed it

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template