【发布时间】:2017-12-12 15:21:55
【问题描述】:
有没有办法获取类的构造函数的调用者函数?
class TestClass {
constructor(options) {
if(<caller> !== TestClass.create)
throw new Error('Use TestClass.create() instead')
this.options = options
}
static async create(options) {
// async options check
return new TestClass(options)
}
}
let test = await TestClass.create()
我尝试了arguments.callee.caller 和TestClass.caller,但出现以下错误:
Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
Uncaught TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
在 Chrome 58 中测试
【问题讨论】:
标签: javascript class async-await ecmascript-2017