开发者

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

开发者 https://www.devze.com 2023-03-02 16:55 出处:网络
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.

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 wo开发者_高级运维rkers 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消