JavaScript Inheritance Method (2)

Source: Internet
Author: User
This article begins to write several tool functions to implement class extension. Each tool function is intended for a specific Writing Method (habit ). This article writes classes by using constructors: attributes (fields) and methods are all mounted on this. The following provides classes as parent classes and child classes respectively. Viewsourceprint? 01 // & nbsp; parent class PersonSyntaxHighlight

This article begins to write several tool functions to implement class extension. Each tool function is intended for a specific Writing Method (habit ). This article writes classes by using constructors: attributes (fields) and methods are all mounted on this. The following provides classes as parent classes and child classes respectively.

View sourceprint? 01 // parent class Person

02 function Person (nationality ){

03 this. nationality = nationality;

04 this. setNationality = function (n) {this. nationality = n ;};

05 this. getNationality = function () {return this. nationality ;};

06}

07

08 // class Man

09 function Man (name ){

10 this. name = name;

11 this. setName = function (n) {this. name = n ;};

12 this. getName = function () {return this. name ;};

13}

 

1. inherit tool function 1


View sourceprint? 1 /**

2 * @ param {Function} subCls subclass

3 * @ param {Function} superCls parent class

4 * @ param {Object} param parent class construction parameters

5 */

6 function extend (subCls, superCls, param ){

7 superCls. call (subCls. prototype, param );

8}

Use the following

View sourceprint? 1 extend (Man, Person, China );

2 var m = new Man (jack );

3 console. log (m. nationality); // China

4 console. log (m. setNationality (Japan ));

5 console. log (m. getNationality (Japan); // Japan

The output shows that Man inherits the attributes of Person and all methods. This inheritance method is very different from that of java,

View sourceprint? 01 class Animal {

02 int legs;

03 Animal (int l ){

04 legs = l;

05}

06 int getLegs (){

07 return legs;

08}

09}

10 public class Person extends Animal {

11 // attribute (field)

12 String name;

13 // Constructor (function)

14 Person (int legs, String name ){

15 super (legs); // call the parent class Constructor

16 this. name = name;

17}

18 // Method

19 String getName (){

20 return this. name;

21}

22 public static void main (String [] args ){

23

24 Person p = new Person (2, "jack ");

25 System. out. println (p. legs );

26}

27}

In Java, the subclass Person calls the parent class constructor super (legs) in its own constructor. When creating an object, the parent class constructor legs: 2 is directly passed in, it is not just to pass your own name: jack. The above JavaScript inheritance is to pass the parent class constructor parameter (the third parameter of the extend function) in extend, rather than passing the parent class constructor parameter in new Man. Good. Simulate Java to implement extend. Here, it cleverly saves the parent class reference on the subclass.

 

2. inherit tool function 2

View sourceprint? 1 /**

2 * @ param {Function} subCls

3 * @ param {Function} superCls

4 */

5 function extend (subCls, superCls ){

6 subCls. supr = superCls;

7}

Or use Person as the parent class to implement the subclass Woman

View sourceprint? 1 function Woman (nationality, name ){

2 Woman. supr. call (this, nationality); // similar to java, call the parent class constructor In the subclass.

3 this. name = name;

4 this. setName = function (n) {this. name = n ;};

5 this. getName = function () {return this. name ;};

6}
Extend (Woman, Person );

Finally, the method for creating an object is similar to that for java, that is, when new is used, the parent class Constructor (nationality: Japan) is passed in.

View sourceprint? 1 var w = new Woman (Japan, lily );

2 console. log (w. nationality); // Japan

3 w. setNationality (U. S. );

4 console. log (w. getNationality (); // U. S.

Related Article

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.