Blog icon indicating copy to clipboard operation
Blog copied to clipboard

原型链继承

Open Harper-Ge opened this issue 1 year ago • 1 comments
trafficstars

Child.prototype = new Parent();
var child1 = new Child();
console.log(child1.getName()) // kevin

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

Harper-Ge avatar Jan 28 '24 10:01 Harper-Ge

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语法把

InnocentLi avatar May 14 '24 16:05 InnocentLi