Home > Web Front-end > JS Tutorial > body text

Detailed explanation of JavaScript inheritance method (2)

零到壹度
Release: 2018-03-22 14:24:21
Original
965 people have browsed it

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;};
}
Copy after login

1, inherit tool function one

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

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
Copy after login

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);
}
}
Copy after login

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;
}
Copy after login

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);
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template