如何在 Javascript 中扩展 “typeof”
How make "typeof" on extends in Javascript?
示例:
class Foo extends Bar {
}
Foo typeof Bar //-> false :(
如何发现Foo
扩展Bar
?
由于 ES6 类彼此继承原型,因此您可以使用isPrototypeOf
Bar.isPrototypeOf(Foo) // true
或者,只需使用通常的instanceof
运算符:
Foo.prototype instanceof Bar // true
// which is more or (in ES6) less equivalent to
Bar.prototype.isPrototypeOf(Foo.prototype)
MDN for typeof
:
typeof 运算符返回一个字符串,指示 未计算的操作数
你需要instanceof
,isPrototypeOf
class Bar{}
class Foo extends Bar {}
var n = new Foo();
console.log(n instanceof Bar); // true
console.log(Bar.isPrototypeOf(Foo)); // true
console.log(Foo.prototype instanceof Bar); // true
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- 扩展移相器按钮类不工作
- chrome扩展中的navigator.geolocation.getCurrentPosition
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- chrome扩展更改主机/域警告
- TableExport jquery插件:文件名和扩展名问题
- 如何在chrome扩展中重定向到html页面
- Chrome扩展没有't在重新加载之前考虑期权价值
- 扩展SVGTextElement时出现Typescript Uncaught TypeError
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- Javascript”;类“;带有参数的扩展
- 试图阻止Chrome通过扩展关闭
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 表达式引擎扩展开发-向呈现的条目添加自定义javascript
- 从选项页面更新chrome扩展清单权限
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- Chrome扩展:遍历不同的页面并收集数据
- DataTable按下键选择扩展/导航
- 扩展jQuery插件以更改配置
- 如何在 Javascript 中扩展 “typeof”