上一篇学习了class的概念,在es5时,对象的继承有好几种,原型链继承,借用构造函数继承,组合继承,原型式继承,寄生式继承以及寄生组合式继承,都是按照函数的形式去集成的,现在class也有它的继承方式,简化了操作。
关键字extends,直接通过这一个关键字就可以实现继承。
class Person{}
class Child extends Person{}
上面代码定义了一个Child类,该类通过extends关键字,继承了Person类的所有属性和方法,所以Child就直接复制了一份Person类。简单的开场方式,是不是比es5的继承方式简单多了,哈。。
在子类中调用super(),说明是表示父类的构造函数,用来新建父类的this对象。
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
say(name) {
console.log(`${name} is saying`)
}
}
class Child extends Person {
constructor(name, age, color) {
super(name, age);
this.color = color
}
jump(name) {
console.log(`${this.name} is jumping`)
}
}
var peter = new Child(‘peter‘,18,‘red‘);
console.log(peter); // Child {name: "peter", age: 18, color: "red"}
peter.jump() // peter is jumping
子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类自己的this对象,必须先通过父类的构造函数完成塑造,得到与父类同样的实例属性和方法,然后再对其进行加工,加上子类自己的实例属性和方法
。如果不调用super方法,子类就得不到this对象。
class Person{}
class Child extends Person{
constructor(){}
}
var peter = new Child() // Uncaught ReferenceError: Must call super constructor in derived class before accessing ‘this‘ or returning from derived constructor
上面代码中,Child继承了父类Person,但是它的构造函数没有调用super方法,导致新建实例时报错。所以说super()是在继承中重要的一环。
另外,如果子类中没有显示constructor,即使不加super,也不会报错,因为class中会默认有constructor,同样的道理,也会有super()
class Child extends Child {}
// 等同于
class Child extends Child {
constructor(...args) {
super(...args);
}
}
ps: ES5的继承和ES6的继承的区别:
ES5 的继承,实质是先创造子类的实例对象this,然后再将父类的方法添加到this上面(Parent.apply(this))。
ES6 的继承机制完全不同,实质是先将父类实例对象的属性和方法,加到this上面(所以必须先调用super方法),然后再用子类的构造函数修改this。
另一个需要注意的地方是,在子类的构造函数中,只有调用super之后,才可以使用this关键字,否则会报错。这是因为子类实例的构建,基于父类实例,只有super方法才能调用父类实例。
class Perosn {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
class Child extends Person {
constructor(name, age, color) {
this.color = color; // ReferenceError:Person is not defined
super(name, age);
this.color = color; // 正确
}
}
super这个关键字,既可以当作函数使用,也可以当作对象使用。在这两种情况
下,它的用法完全不同。
class A {
constructor() {
console.log(new.target.name);
}
}
class B extends A {
constructor() {
super();
}
}
new A() // A
new B() // B
super虽然代表了父类A的构造函数,但是返回的是子类B的实例,即super内部的this指的是B,因此super()在这里相当于A.prototype.constructor.call(this)。继承了A的this为B所用。
class Person {}
class Child extends Person {
jump() {
super(); // error
}
}
上面的例子,在Child子类中的constructor之外的方法中用了super(),会报错,因为super函数只能用在构造函数constructor中。
class Person {
constructor(name,age) {
this.name = name;
this.age = age;
}
say(){
console.log(this.age)
}
p(){
return 2
}
}
class Child extends Person {
constructor(name,age, color) {
super(name,age); //该super代表的是用函数的形式
this.age = 25;
this.color = color;
console.log(super)//使用super的时候,必须显式指定是作为函数、还是作为对象使用,否则会报错。如果只写super,没法判断是对象还是方法
console.log(super.p());
}
say1(){
super.say()
}
}
var peter = new Child(‘peter‘,18,‘red‘)
peter.p() // 2
peter.say1() // 25
super.p() == Person.prototype.p()
如上方法内部的this指向当前的子类实例
。super.say()虽然调用的是Person.prototype.say(),但是Person.prototype.say()内部的this指向子类Child的实例,导致输出的是25,而不是2。也就是说,实际上执行的是super.say.call(this)
。如上由于this指向子类实例,所以如果通过super对某个属性赋值,这时super就是this,赋值的属性会变成子类实例的属性。
class A {
constructor() {
this.x = 1;
}
}
class B extends A {
constructor() {
super();
this.x = 2;
super.x = 3;
console.log(super.x); // undefined
console.log(this.x); // 3
}
}
let b = new B();
super.x赋值为3,这时等同于对this.x赋值为3。而当读取super.x的时候,读的是A.prototype.x,所以返回undefined。class A {
constructor() {
this.p = 2;
}
}
A.prototype.q = 4
class B extends A {
m() {
return super.p;
}
n(){
return super.q
}
}
let b = new B();
b.m() // undefined
b.q // 4
p是父类A实例的属性,但super.p就引用不到它。属性q是定义在A.prototype上面的,所以super.q可以取到它的值。
class Parent {
static myMethod(msg) {
console.log(‘static‘, msg);
}
myMethod(msg) {
console.log(‘instance‘, msg);
}
}
class Child extends Parent {
static myMethod(msg) {
super.myMethod(msg);
}
myMethod(msg) {
super.myMethod(msg);
}
}
Child.myMethod(1); // static 1
var child = new Child();
child.myMethod(2); // instance 2
在子类的静态方法中通过super调用父类的方法时,方法内部的this指向当前的子类,而不是子类的实例。
用来判断一个类是否继承了另一个类。
class A {
static hello() {
console.log(‘hello world‘);
}
}
class B extends A {
constructor(color){
super();
this.color = color
}
}
var red = new B(‘red‘);
red instanceof B // true
red instanceod A // true
Object.getPrototypeOf(B) === A // true
B.hello() // hello world
由上面的例子可知:red既是A的实例又是B的实例,通过Object.getPrototypeOf(B)判断了是继承A的,同样的静态方法也可以继承的
class A {}
class B extends A {}
B.__proto__ === A // true
B.prototype.__proto__ === A.prototype // true
原因如下:
Object.setPrototypeOf = function (obj, proto) {
obj.__proto__ = proto;
return obj;
}
class A {}
class B {}
// B 的实例继承 A 的实例
Object.setPrototypeOf(B.prototype, A.prototype);
// 等同于
B.prototype.__proto__ = A.prototype;
// B 继承 A 的静态属性
Object.setPrototypeOf(B, A);
// 等同于
B.__proto__ = A;
这两条继承链,可以这样理解:作为一个对象,子类(B)的原型(__proto__属性)是父类(A);作为一个构造函数,子类(B)的原型对象(prototype属性)是父类的原型对象(prototype属性)的实例。
另外还有两种情况:
class A extends Object {}
A.__proto__ === Object // true
A.prototype.__proto__ === Object.prototype // true
class A {}
A.__proto__ === Function.prototype // true
A.prototype.__proto__ === Object.prototype // true
A作为一个基类(即不存在任何继承),就是一个普通函数,所以直接继承Function.prototype。但是,A调用后返回一个空对象(即Object实例),所以A.prototype.__proto__指向构造函数(Object)的prototype属性。
子类实例的__proto__属性的__proto__属性,指向父类实例的__proto__属性。也就是说,子类实例的原型的原型,是父类的原型
。
class Point{
constructor(x,y){
this.x = x;
this.y = y;
}
printName(){
console.log(‘hahahah‘)
}
}
class ColorPoint extends Point{
constructor(x,y,color){
super(x,y);
this.color = color
}
printName(){
super.printName()
}
}
var p1 = new Point(2, 3);
var p2 = new ColorPoint(2, 3, ‘red‘);
p2.__proto__ === p1.__proto__ // false
p2.__proto__.__proto__ === p1.__proto__ // true
p2.__proto__.__proto__.printName = function () {
console.log(‘Ha‘);
};
p1.printName() // "Ha"
由例子可知:
class类是参考阮一峰老师的es6学习的,虽然文章中有很多都是copy过来的,但是有几部分就是通过简化例子,使通俗易懂,方便自已以后复习。class类在以后的规范中会长使用,里面的原型及实例的原型都要搞明白指的是谁,还有super关键字的运用,有两种方式。场景不一样,运用不一样。
这篇文章,如果有错误的请私信或评论,大家一起进步,我把知识点放到github里了,满意的话给个star,谢谢。
阮一峰ES6http://es6.ruanyifeng.com/#docs/class-extends
原文:https://www.cnblogs.com/sqh17/p/10236753.html