Home >Web Front-end >JS Tutorial >Detailed explanation of JavaScript inheritance method (2)

Detailed explanation of JavaScript inheritance method (2)

零到壹度
零到壹度Original
2018-03-22 14:24:211004browse

This article starts by writing a few tool functions to implement class extensions. Each utility function is targeted at a specific way (habits) of writing classes. This article writes classes according to the constructor method: attributes (fields) and methods are all hung on this. The following classes are provided respectively as parent classes and subclasses.

/

/  父类Person
function Person(nationality) {
    this.nationality = nationality;
    this.setNationality = function(n) {this.nationality=n;};
    this.getNationality = function() {return this.nationality;};
}
// 子类Man
    function Man(name) {
    this.name = name;
    this.setName = function(n){this.name=n;};
    this.getName = function(){return this.name;};
}

1, inherit tool function one

/**
 * @param {Function} subCls 子类
 * @param {Function} superCls 父类
 * @param {Object} param 父类构造参数
 */
function extend(subCls, superCls, param) {
superCls.call(subCls.prototype, param);
}

Use the following

extend(Man, Person, 'China');
var m = new Man('jack');
console.log(m.nationality);//China
console.log(m.setNationality('Japan'));
console.log(m.getNationality('Japan'));//Japan

output You can see that Man inherits the properties and all methods of Person. This inheritance method is very different from Java.

class Animal {
int legs;
Animal(int l) {
legs = l;
}
int getLegs() {
return legs;
}
}
public class Person extends Animal{
//属性(字段)
String name;
//构造方法(函数)
Person(int legs, String name) {
super(legs);//调用父类构造器
this.name = name;
}
//方法
String getName() {
return this.name;
}
public static void main(String[] args) {
Person p = new Person(2,"jack");
System.out.println(p.legs);
}
}

In Java, the subclass Person calls the parent class construction method super(legs) in its own construction method, and directly sets the parent class construction parameters when creating an object. Pass legs:2 in, not just your own name:jack. The above JavaScript inheritance is to pass the parent class construction parameters (the third parameter of the extend function) when extending, not when new When manning, pass the parent class construction parameters. Okay, simulate Java to implement extend. Here, the parent class reference is cleverly temporarily stored in the subclass.

2, inherit tool function two

/**
 * @param {Function} subCls
 * @param {Function} superCls
 */
function extend(subCls, superCls) {
subCls.supr = superCls;
}

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

function Woman(nationality, name) {
Woman.supr.call(this, nationality);//和java有点类似哦,在子类中调用父类构造器
this.name = name;
this.setName = function(n){this.name=n;};
this.getName = function(){return this.name;};
}
extend(Woman, Person);

Finally, the method of creating an object is similar to that of Java, that is, when new, the parent class construction parameter (nationality: Japan) is passed in at the same time.

var w = new Woman('Japan', 'lily');
console.log(w.nationality);//Japan
w.setNationality('U.S.A');
console.log(w.getNationality());//U.S.A

The above is the detailed content of Detailed explanation of JavaScript inheritance method (2). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn