Question

I recently switched all my projects to ECMAScript 5 strict mode (i.e., add "use stricts"; at top of every JS file), however the following MDC example code works everywhere except on Chrome 11.

https://developer.mozilla.org/en/Using_web_workers#The_JavaScript_code

The code in web workers will invoke error

Uncaught ReferenceError: onmessage is not defined.

I tried to use var onmessage as a workaround, it would work in Chrome 11 but not in Firefox 4. I shouldn't be using var anyway coz AFAIK onmessage is a global variable just like window, redefining it makes no sense.

What should I do?

Was it helpful?

Solution

You shouldn't switch to strict mode if you don't know what it means. For one, you can't specify implicit global variables, which is your problem. Specify self.onmessage.

OTHER TIPS

Then Chrome 11 is the only one who is working as the ES5 is expecting it. See https://developer.mozilla.org/en/JavaScript/Strict_mode#Simplifying_variable_uses

You have two possibilities:

var onmessage = function(...

or

function onmessage(...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top