Chrome 11 complains about onmessage is not defined for worker.js in ECMAScript 5 strict mode

StackOverflow https://stackoverflow.com/questions/5867539

문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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(...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top