Summary of several ways to inherit JS object

Source: Internet
Author: User

Today I learned a bit of JS inheritance, JS in the inheritance of the main four kinds, prototype inheritance, constructor inheritance, call/apply inheritance and es6 inheritance of extend.
1. Prototype Inheritance:
Prototype inheritance mainly uses the prototype of the JS object to refer to the parent class's constructor to copy the parent class's methods.

//define a person class     functionPerson (name) { This. name=name; }      //Greetingperson.prototype.sayhello=function() {alert ("Hello,my name is" + This. Name); }           //defining the Man class, inheriting the person class    functionMan (name,age) { This. name=name;  This. age=Age ; } Man.prototype=NewPerson (); varman=NewMan ("The Old King next Door", 30); Man.sayhello ();

2. Constructor inheritance
The constructor of the parent class is called in the subclass to complete the inheritance.

 //define a person class     functionPerson (name) { This. name=name;  This. sayhello=function() {alert ("Hello,my name is" + This. Name); }      }       //defining the Man class, inheriting the person class    functionMan (name,age) { This. constructor=Person ;  This. Constructor (name);  This. age=Age ; }    varman=NewMan ("The Old King next Door", 30); Man.sayhello ();

3.call/apply inheritance
Using the Call/apply method to invoke the parent class constructor to implement inheritance

 //define a person class     functionPerson (name) { This. name=name;  This. sayhello=function() {alert ("Hello,my name is" + This. Name); }      }       //defining the Man class, inheriting the person class    functionMan (name,age) {Person.call ( This, name);  This. age=Age ; }    varman=NewMan ("The Old King next Door", 30); Man.sayhello ();

4.extends inheritance
Use ES6 to define the method of the class, similar to the way Java defines the class to implement inheritance, note that some browsers are incompatible--

' Use strict '; //define a person classclass person{//constructor FunctionConstructor (name) { This. name=name; } SayHello () {alert ("My name is" + This. Name); }} class Man extends person{constructor (name,age) {//calling the parent class constructorsuper (name);  This. age=Age ; }            }    varman=NewMan ("The Old King next Door", 30); Man.sayhello ();

Summary of several ways to inherit JS object

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.