JS Object-oriented-inheritance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.