mqyqingfeng / Blog

冴羽写博客的地方,预计写四个系列:JavaScript深入系列、JavaScript专题系列、ES6系列、React系列。
30.53k stars 4.7k forks source link

原型链继承 #327

Open Harper-Ge opened 5 months ago

Harper-Ge commented 5 months ago
Child.prototype = new Parent();
var child1 = new Child();
console.log(child1.getName()) // kevin

这一块好像最后还会打印undefined,是因为es6规则发生了改变吗

InnocentLi commented 1 month ago
class Parent {
    constructor(name = 'Kevin') {
        this.name = name;
    }

    getName() {
        return this.name;
    }
}

class Child extends Parent {
    constructor(name) {
        super(name);
    }
}

var child1 = new Child();
console.log(child1.getName()); // 输出 'Kevin'

你这么写才是es6语法把