Akhil F
Akhil F

Reputation: 7740

What is the function constructor in Node.js?

In the browser (chrome at least) functions are instances of Function

setTimeout instanceof Function
// true

However in node, they are not

setTimeout instanceof Function
// false

So what is setTimeout's constructor if not Function?

Upvotes: 11

Views: 845

Answers (1)

Oriol
Oriol

Reputation: 288100

It seems the constructor is Function, but the one from another realm.

If you run this code

console.log(Object.getOwnPropertyNames(setTimeout.constructor.prototype));

you get an array with the typical Function.prototype methods like call, apply and bind.

So I guess it's somewhat analogous to what happens in web browsers when you borrow setTimeout from an iframe:

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

Upvotes: 3

Related Questions