Defining the Parent class
Prototype mode and constructor combination function person (name,age,job) {//constructor pattern defines instance properties THIS.name = Name;this.age = Age;this.job = Job;} Person.prototype = {//prototype mode definition method and Shared properties Constructor:person, //rewrite prototype changed constructor, fix back Sayname:function () {alert ( THIS.name);},sayjob:function () {alert (this.job);}}
Defining subclasses
Define the Singer class and specify the job to be singerfunction Singer (name,age,song) {Person.call (this,name,age, ' Singer '); this.song = song;}
Each call to the singer constructor sets the singer job to singer and adds a property song.
Singer Inherit person
Singer.prototype = new Person (); Singer.prototype.constructor = Singer; Singer.prototype.saySong = function () {alert (this.song);}
The first step is to set singer's prototype as the instance object of person, the second step is to fix constructor point, because singer's prototype equals person, so its constructor points to the person function, The third step adds a method for singer objects.
Create a Singer Object
var = new Singer (' Taylor Swift ', TaylorSwift, ' love Stories '); Taylorswift.saysong (); "Love Stories" taylorswift.sayjob (); "Singer"
JS Object-oriented-inheritance