为了账号安全,请及时绑定邮箱和手机立即绑定

小谈ES6中的class与继承

标签:
JavaScript
ES6 class

class是创建类对象与实现类继承的语法糖,旨在让ES5中对象原型的写法更加清晰,易读。

class语法

基本使用

class Point {
    constructor(x = 0, y = 0) {
        this.x = x;
        this.y = y;
    }

    toString() {
        return this.x + ':' + this.y;
    }
}

let point = new Point();
console.log(point.toString());

一些细节

  1. ES6类的constructor函数相当于ES5的构造函数
  2. 类中定义方法时,前面不用加function,后面不得加,,方法全部都是定义在类的prototype属性中。
  3. 类的内部定义的所有方法都是不可枚举的
  4. 类和模块内部默认采取严格模式

另外,下面这种写法是无效的。class内部只允许定义方法,不允许定义属性(包括静态属性)

class Foo{
    bar:2
}

属性名可以使用表达式

let foo = 'toString';
... ...
[foo] () {
        return this.x + ':' + this.y;
}
... ...

constructor方法

类必须定义constructor属性,如果没有显式定义,一个空的constructor方法会被默认添加。constructor方法默认返回实例对象this,但也可以指定返回另一个对象。

class B{};
var b = new B();
B.prototype.constructor = b.constructor;

立即执行class

class也可以写成表达式的形式,这样可以写出立即执行的Class。

let point = new class{
    constructor(x = 0, y = 0) {
        this.x = x;
        this.y = y;
    }

    toString() {
        return this.x + ':' + this.y;
    }
}(1, 2);

console.log(point.toString());
继承

基本用法

class Animal {
    constructor(name = 'John Doe', species = '物种'){
        this.name = name;
        this.species = species;
    }

    sayHello(){
        console.log('hello',this.name,this.species)
    }
}

class Sheep extends Animal{
    constructor(name = 'Jimmy',species = '羊'){
        super(name, species);
    }

    sayHello(){
        console.log('child');
        super.sayHello()
    }
}

let sheep = new Sheep('Tom');
sheep.sayHello();

输出

child
hello Jimmy 羊

super

子类必须在constructor方法里调用super方法,否则不能新建实例。这是因为子类没有属于自己的this对象,而是继承了父类的this对象而对其进行加工。显然,只有调用了super方法之后,才可以使用this

而super本身指代的是父类的实例对象,我们可以使用super.的方式调用父对象的方法。此外,由于对象总是继承于其它对象,所以可以在ES6的任何一个对象中使用super关键字。

let obj = {
    arr : [1,2,3],
    toString(){
        console.log('super:'+super.toString.apply(this.arr));
    }
};

obj.toString()

这里的super显然指向的是Object实例对象。

隐式constructor

如果子类没有显式的定义constructor,那么下面的代码将被默认添加

constructor(...args){
    super(...args)
}

ES5继承和ES6继承的区别

在ES5中,继承实质上是子类先创建属于自己的this,然后再将父类的方法添加到this(也就是使用Parent.apply(this)的方式)或者this.__proto__(即Child.prototype=new Parent())上。

而在ES6中,则是先创建父类的实例对象this,然后再用子类的构造函数修改this。

instanceof

使用继承的方式创建的对象既是父类的实例,也是子类的实例。

sheep instanceof Sheep // =>true
sheep instanceof Animal // => true

继承链

子类Sheep作为一个函数对象,其__proto__属性等于父类Animal

console.log(Object.getPrototypeOf(Sheep));

输出

class Animal {
    constructor(name = 'John Doe', species = '物种'){
        this.name = name;
        this.species = species;
    }

    sayHello(){
        console.log('hello',this.…

就继承关系而言,子类Sheep的prototype属性(即子类实例sheep的__proto__属性)的__proto__属性,等于父类Animal的prototype属性。

静态方法
class Foo{
    static say(){
        console.log('I am saying: ');
    }
}

class Bar extends Foo{
    sayHello(){
        super.say(); // 报错,因为sayHello不是静态方法
        console.log('hello');
    }

    static singHello(){
        super.say();
        console.log('hello')
    }
}

Bar.say();
let bar = new Bar();
bar.sayHello();

使用static关键字可以在类中定义类的静态方法,子类自动继承父类的静态方法。

在子类的静态方法中,可以使用super调用父类的静态方法。注意,子类静态方法中的super只能调用父类的静态方法,而不能调用父类的其它方法,反之亦然,子类的实例方法中的super只能调用父类的实例方法,不能调用其静态方法,如果想要调用父类的静态方法,可以使用父类名.方法的方式。

混入

使用Mixin可以实现多重继承

点击查看更多内容
19人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
316
获赞与收藏
3231

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消