有没有比arguments.callee.name
更安全的替代品?
我的团队正在为Chromecast应用程序开发一个大量使用类继承的库。我们希望确保在扩展类时覆盖某些方法。
为此,我们想出了一个辅助函数来存根这些方法:
const methodNotImplemented = (method) => throw new Error(`Method ${method} not implemented`);
现在,如果我们可以这样做以避免手动传递方法名称,那将非常酷,但是我从eslint等处收到了各种警告。
const methodNotImplemented = (method) => throw new Error(`Method ${method || argument.callee.name} not implemented`);
这是否可以,因为代码只能在 Chrome 中运行,或者是否有任何人都知道的替代方案?
用法示例:
class Base {
// method that should be overridden by application
requiredMethod() {
methodNotImlemented(' Base.requiredMethod')
};
initialize() {
requiredMethod();
}
}
我们希望确保方法在这里被覆盖
class MyPlugin extends Base {
}
使用new Error().stack
似乎是最好的解决方案。谢谢@vlaz。