5

I am getting slightly confused about all this...

Chrome and Firefox both tell me different things, and I couldn't find any part in the spec that mentioned it, but:

in Chrome:

Object instanceof Function // true Function instanceof Object // true Worker instanceof Object // true Worker instanceof Function // false <- WTF??? 

in FireFox:

Object instanceof Function // true Function instanceof Object // true Worker instanceof Object // false Worker instanceof Function // false 

of course, an initialised new Worker() is both a Worker and an Object, but why is the Worker constructor not a function?

1
  • Web-browsers are not required to implement the Worker interface as a native ECMAScript function. However, the trend in modern web-browsers is to implement more and more interfaces as native ECMAScript object types. Btw, in my Firefox (latest, on Windows), Worker instanceof Function does evaluate to true. Commented Nov 11, 2012 at 21:10

2 Answers 2

5

Worker IS of type function. You can check that using the typeof operator. However, it does not inherit the prototype of the Function constructor, hence it is not an instanceof Function.

Here's a more practical example:

function fun(){}; Function.prototype.foo = 'my custom Function prototype property value'; console.log(fun.foo); //my custom Function prototype property value console.log(fun instanceof Function); //true console.log(typeof Worker); //function, the constructor console.log(Worker.foo); //undefined, this host constructor does not inherit the Function prototype console.log(Worker instanceof Function); //false var worker = new Worker('test.js'); console.log(typeof worker); //object, the instance console.log(worker.foo); //undefined, instance object does not inherit the Function prototype console.log(worker instanceof Function); //false​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

From MDN:

The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.

Worker does not inherit the Function constructor's prototype, hence it is not an instance of Function.


Here's an example using the typeof operator to check if the user's browser supports the Web Workers API:

if (typeof window.Worker !== 'undefined') { alert('Your browser supports Web Workers!'); } else { alert('Sorry, but no.'); //too lazy to write a proper message for IE users }​ 

Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

No problem. =] Oh there was a suggested edit while I was editing the tests, re-submit the edit suggestion if the editor sees this. =)
@phenomnomnominal Also, if you're just checking if the browser supports the Worker API, you can use if (typeof window.Worker !== 'undefined') too. =]
2

Worker is a host object, not part of the language spec. It doesn't need to meet all the requirements of the language. There's nothing to say that it must represent itself as having been created from the Function or any other constructor.

The others like Object and Function do because the language spec requires it of them.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.