在浏览器中(至少是chrome)函数是Function
setTimeout instanceof Function
// true
但是在 Node 中,它们不是
setTimeout instanceof Function
// false
如果不是Function
,那么setTimeout
的构造函数是什么?
最佳答案
看来构造函数是Function
,不过是另一个领域的构造函数。
如果你运行这段代码
console.log(Object.getOwnPropertyNames(setTimeout.constructor.prototype));
您会得到一个包含典型 Function.prototype
方法的数组,例如 call
、apply
和 bind
。
所以我想这有点类似于从 iframe 借用 setTimeout
时在网络浏览器中发生的情况:
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
var win = iframe.contentWindow;
console.log(win.setTimeout instanceof Function); // false
console.log(win.setTimeout instanceof win.Function); // true
关于javascript - Node.js 中的函数构造函数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40444294/