es6--Class类与继承的用法

2020-01-14  本文已影响0人  花拾superzay

一个demo说明class类与继承的用法

//父类
class Point {
    //构造函数
    constructor(x, y) {
        this.x = x;
        this.y = y;
        return this
    }
    //原型
    toString() {
        return `(${this.x},${this.y})`
    }
}

//子类
class Subpoint extends Point {
    //子类构造函数
    constructor(x, y, z) {
        super(x, y);//先实例化父类属性
        this.z = z;
        return this
    }
    //子类原型
    toString1() {
        return `(${this.x},${this.y},${this.z})`
    }
}
上一篇 下一篇

猜你喜欢

热点阅读