跳到主要内容
CNode

继承EventEmitter的子类中,this未定义?

问答
Iisky0824发布于 10 年前最后回复 10 年前
342910

"use strict"; var EventEmitter = require('events'); class Rectangle extends EventEmitter { constructor (width, height) { this._width = width this._height = height } set width (width) { this._width = width } get width () { return this._width } set height (height) { this._height = height } get height () { return this._height } get area () { return this._width * this._height } } var r = new Rectangle(50, 20); 运行上面代码的时候,已知提示this未定义,把extends EventEmitter注释掉之后,就正常了,这是为何?

查看回复

回复 (3)

I
isky0824#310 年前
引用 ChopperLee2011你忘记了super();

@ChopperLee2011 问题已解决,在constructor 函数里的第一行添加super();就可以了,谢谢!

I
isky0824#210 年前
引用 ChopperLee2011你忘记了super();

@ChopperLee2011 这个要怎么做?我新手,求指教,谢谢!

C
ChopperLee2011#110 年前

你忘记了super();

参与回复

登录后即可参与回复。登录