Subclasses in JS inherit the methods and properties of the parent class

Source: Internet
Author: User

last time we talked about a simple inheritance, this time let's talk a little more complicated, that is, let the subclass inherit the properties and methods of the parent class, assuming that the person is the parent class, the worker (worker) is a subclass, and let the worker inherit the properties and methods of people:
Parent class:
function Person (name,age)
{
This.name=name;
This.age=age;
}

person.prototype.showname=function ()
{
alert (this.name);
}

function Worker (Name,age,job)
{
Person.call (this,name,age)//This is where the call method inherits the Name property of the person and the Age method
Worker.job=job;
}

worker.prototype=new person ()//through New person (), returns a person object with the ShowName method, so, The worker class inherits the ShowName method of the person class .
worker.prototype.showjob=function ()
{
alert (this.job)
}
var ow=worker (' Kate ', ' 23 ', ' Handyman ');
alert (ow.name)//This place, set the Name property to the worker, but added the name attribute to his parent, but still pops up Kate, which means he inherits the parent's Name property
alert (ow.age)//This place also inherits the age property of the parent
ow.showname ()//can be executed, indicating the method that inherits the person
ow.showjob ()//Only the worker class has this method, the person class does not have
we have nothing to understand, who can ask the question ha

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.