Javascript 类继承无法正常工作

Mig*_*rso 2 javascript inheritance class

尝试创建一些 Javascript 类和父类,不确定我是否正确执行此操作,但子类中的 super() 无法正常工作。尝试使 DivElement 中的内容正常工作,但它始终返回未定义。

代码:

 class HTMLElement{
    constructor(tag, content){
        this.tag = tag;
        this.content = content;
    }

    render(){
        return `<${this.tag}>${this.content}</${this.tag}>`;
    }

class DivElement extends HTMLElement{
constructor(content){
    super(content);
    this.tag = 'div';
   }

 }

let div1 = new DivElement('test');
console.log(div1);
console.log(div1.render());
Run Code Online (Sandbox Code Playgroud)

Gly*_*ine 5

超级调用应该与目标方法的签名匹配。它应该是super('div', content);

class HTMLElement{
    constructor(tag, content){
        this.tag = tag;
        this.content = content;
    }

    render(){
        return `<${this.tag}>${this.content}</${this.tag}>`;
    }
 }

class DivElement extends HTMLElement{
    constructor(content){
    super('div', content);  // changed here
    this.tag = 'div';
   }

 }

let div1 = new DivElement('test');
console.log(div1);
console.log(div1.render());
// <div>test</div>
Run Code Online (Sandbox Code Playgroud)