JS class object Implementation of several ways to continue

Source: Internet
Author: User

0. ES6 can be inherited directly using Class,extends.

1. Prototype inheritance

1       2 Parent class:3 4       functionpersion (name,age) {5          This. Name =name; 6          This. Age =Age ; 7        } 8  9        //prototype object properties of the parent classTenPersion.prototype.id = 10;  One  A  -        //sub-class -        functionBoy (Sex) { the          This. Sex =sex;  -        }   -        //Inheritance Implementation -Boy.prototype =NewPersion (' Yuri ', 30);//to initialize the parent class is not graceful ~ +        varb =NewBoy ();  -   +Alert (B.name)//Yuri AAlert (b.id)//10

The characteristic of this prototype inheritance is that it inherits both the template of the parent class and the prototype object of the parent class. The disadvantage is that the parent class instance passes the parameter, not the subclass instantiation pass argument, does not conform to the general language wording.

2. Class inheritance (inherit from constructor)

//Parent Classfunctionpersion (name,age) { This. Name =name;  This. Age =Age ; }  //prototype object properties of the parent classPersion.prototype.id = 10; //sub-classfunctionBoy (name,age,sex) {//Call Apply Implementation inheritancePersion.call ( This, Name,age);  This. Sex =Sex }

Boy.prototype = new Persion ();//This opens the prototype chain object and can inherit it. varb =NewBoy (' Yuri ', 30, ' male '); Alert (b.name)//YuriAlert (b.id)//Undinfind The prototype object of the parent class does not inherit

This prototype inherits the following characteristics: inherits the template of the parent class, and does not inherit the prototype object of the parent class. The advantage is that it facilitates the transfer of the child class instance, and the disadvantage is that it does not inherit the parent class's prototype object

3. Minimalism

//defining the Parent classvarperson ={createNew:function() {varperson = {}; Person.name= ' Yuri '; Person.sleep=function() {alert ("Sleep in");      }; returnPerson//returning This object}};//sub-classvarsuperperson={createNew:function(){             varPrivate//You can also define private properties.             varsuperperson=person.createnew (); //Superperson is going to use the methods and properties of the parent class.            //add your own attributes at the same timesuperperson.fly=function() {} Superperson.power=""; returnSuperperson; }}      vartest=superperson.createnew ();//you can implement inheritance by using the createnew of subclasses.Console.log (Test.fly, Test.name, Test.power)

Not using any of the Prototype,apply,call

JS class object Implementation of several ways to continue

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.