如何使用ES6 super调用child构造函数中的parent方法?
How to use ES6 super to call parent method in child constructor?
情况:
我正在扩展 Node.js (v. 8.4.0) Error object 附加属性(时间戳、id),然后扩展这个 object 以获得更精细的错误处理.
class MyError extends Error {
constructor (msg) {
super(msg);
this.id = uuid();
this.timestamp = Date.now();
// I reckon this can be replaced by this.init(this) ?
this.name = this.constructor.name;
Error.captureStackTrace && Error.captureStackTrace(this, this.constructor);
}
init (self) {
self.name = self.constructor.name;
Error.captureStackTrace && Error.captureStackTrace(self, self.constructor);
}
}
我希望能够不重复 child 错误中的 Error.captureStackTrace
和 this.name
调用。所以我创建了一个在 child 中使用的初始化函数:
class GranularError extends MyError {
constructor (msg) {
super(msg);
this.type = "error";
this.status = 500;
this.code = "internalServerError";
super.init(this);
}
}
GranularError
然后将再次扩展以获得 MoreGranularError
等。这就是为什么我想保持干燥。
问题:
当抛出 GranularError 或 MoreGranularError 时,它会失败并显示
TypeError: (intermediate value).init is not a function
我主要阅读了以下资源,但无法将它们应用到问题中。感谢您的帮助。
Call parent function which is being overridden by child during constructor chain in JavaScript(ES6)
Parent constructor call overridden functions before all child constructors are finished
http://2ality.com/2015/02/es6-classes-final.html#referring_to_super-properties_in_methods
我不知道您收到此错误是什么,但没有必要创建 init
函数。 this.name
和 Error.captureStack
的东西也将在子实例中起作用,因为 this
指的是子实例。
换句话说,您正在尝试解决一个不存在的问题。
class MyError extends Error {
constructor (msg) {
super(msg);
this.id = Math.random();
this.timestamp = Date.now();
this.name = this.constructor.name;
Error.captureStackTrace && Error.captureStackTrace(this, this.constructor);
}
}
class GranularError extends MyError {
constructor (msg) {
super(msg);
this.type = "error";
this.status = 500;
this.code = "internalServerError";
}
}
console.dir(new GranularError("this is the error message"));
情况:
我正在扩展 Node.js (v. 8.4.0) Error object 附加属性(时间戳、id),然后扩展这个 object 以获得更精细的错误处理.
class MyError extends Error {
constructor (msg) {
super(msg);
this.id = uuid();
this.timestamp = Date.now();
// I reckon this can be replaced by this.init(this) ?
this.name = this.constructor.name;
Error.captureStackTrace && Error.captureStackTrace(this, this.constructor);
}
init (self) {
self.name = self.constructor.name;
Error.captureStackTrace && Error.captureStackTrace(self, self.constructor);
}
}
我希望能够不重复 child 错误中的 Error.captureStackTrace
和 this.name
调用。所以我创建了一个在 child 中使用的初始化函数:
class GranularError extends MyError {
constructor (msg) {
super(msg);
this.type = "error";
this.status = 500;
this.code = "internalServerError";
super.init(this);
}
}
GranularError
然后将再次扩展以获得 MoreGranularError
等。这就是为什么我想保持干燥。
问题:
当抛出 GranularError 或 MoreGranularError 时,它会失败并显示
TypeError: (intermediate value).init is not a function
我主要阅读了以下资源,但无法将它们应用到问题中。感谢您的帮助。
Call parent function which is being overridden by child during constructor chain in JavaScript(ES6)
Parent constructor call overridden functions before all child constructors are finished
http://2ality.com/2015/02/es6-classes-final.html#referring_to_super-properties_in_methods
我不知道您收到此错误是什么,但没有必要创建 init
函数。 this.name
和 Error.captureStack
的东西也将在子实例中起作用,因为 this
指的是子实例。
换句话说,您正在尝试解决一个不存在的问题。
class MyError extends Error {
constructor (msg) {
super(msg);
this.id = Math.random();
this.timestamp = Date.now();
this.name = this.constructor.name;
Error.captureStackTrace && Error.captureStackTrace(this, this.constructor);
}
}
class GranularError extends MyError {
constructor (msg) {
super(msg);
this.type = "error";
this.status = 500;
this.code = "internalServerError";
}
}
console.dir(new GranularError("this is the error message"));