phe*_*nal 5 javascript web-worker
我对这一切感到有些困惑......
Chrome和Firefox都告诉我不同的东西,我找不到提到它的规范中的任何部分,但是:
在Chrome中:
Object instanceof Function // true
Function instanceof Object // true
Worker instanceof Object // true
Worker instanceof Function // false <- WTF???
Run Code Online (Sandbox Code Playgroud)
在FireFox中:
Object instanceof Function // true
Function instanceof Object // true
Worker instanceof Object // false
Worker instanceof Function // false
Run Code Online (Sandbox Code Playgroud)
当然,初始化的新Worker()既是Worker又是Object,但为什么Worker构造函数不是函数?
Worker IS类型为函数。您可以使用typeof运算符进行检查。但是,它不继承 Function 构造函数的原型,因此它不是 Function instanceof。
这是一个更实际的例子:
\n\nfunction fun(){};\nFunction.prototype.foo = \'my custom Function prototype property value\';\nconsole.log(fun.foo); //my custom Function prototype property value\nconsole.log(fun instanceof Function); //true\n\nconsole.log(typeof Worker); //function, the constructor\nconsole.log(Worker.foo); //undefined, this host constructor does not inherit the Function prototype\nconsole.log(Worker instanceof Function); //false\n\nvar worker = new Worker(\'test.js\');\nconsole.log(typeof worker); //object, the instance\nconsole.log(worker.foo); //undefined, instance object does not inherit the Function prototype\nconsole.log(worker instanceof Function); //false\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\nRun Code Online (Sandbox Code Playgroud)\n\n来自MDN:
\n\n\n\n\n该
\ninstanceof运算符测试对象的原型链中是否具有构造函数的原型属性。
Worker 不继承 Function 构造函数的原型,因此它不是 Function 的实例。
\n\n以下是使用typeof运算符检查用户浏览器是否支持 Web Workers API 的示例:
if (typeof window.Worker !== \'undefined\') {\n alert(\'Your browser supports Web Workers!\');\n} else {\n alert(\'Sorry, but no.\'); //too lazy to write a proper message for IE users\n}\xe2\x80\x8b\nRun Code Online (Sandbox Code Playgroud)\n\n\n