new.target
用于检测 方法或构造函数 是否是通过new
被调用的- 通过
new
初始化的 构造函数中,new.target
返回一个指向构造函数的引用 - 普通函数 则 返回
undefined
1 | class Point { |
1 | class Parent { |
- 检测 是否通过 new 创建 的实例
1
2
3function classCheck (instance, constructor) {
if (!(instance instanceof constructor)) throw new Error('should exec with new keyword')
}