子类中的"ReferenceError:this not defined"

fad*_*bee 2 javascript extends node.js ecmascript-6

我有一些代码:

"use strict";

class Node {
  constructor() {

  }
}

class Person extends Node {
  constructor() {

  }
}

const fred = new Person();
Run Code Online (Sandbox Code Playgroud)

在Node v4.2.4中运行它会给出错误:

ReferenceError: this is not defined
    at Person (/home/chris/test.js:12:3)
Run Code Online (Sandbox Code Playgroud)

其中第12行是Person :: constructor的右括号.

为什么我不能扩展Node类?

Jos*_*ung 6

你需要调用超级构造函数:

class Person extends Node {
  constructor() {
    super();
  }
}
Run Code Online (Sandbox Code Playgroud)

作为参考,我实际上在es6fiddle上测试了你的代码,它在控制台中给出了一个非常好的描述性错误消息.

Uncaught SyntaxError:unknown:第10行:派生的构造函数必须调用super()

   8 | 
   9 | class Person extends Node {
> 10 |   constructor() {
     |   ^
  11 | 
  12 |   }
  13 | }
Run Code Online (Sandbox Code Playgroud)