문제

Environment: SeaMonkey (FireFox 20.0)

I'm trying to do a simple sort: Either inside a worker itself OR inside its postMessage handler.

(All code samples from inside the top level (.html) file).

works:

function a() {
  var xyzzy = [40, 1, 5, 200];
  xyzzy.sort();
};

fails: (Error: TypeError: xyzzy.sort is not a function)

var lrw0 = new Worker('lrw0.js');
lrw0.onmessage = function (event) {
  var xyzzy = [40, 1, 5, 200];
  xyzzy.sort();
};

After the postMessage event the handler has the same context and scope as did the worker. Fair enough. Needless to say the sort fails inside the worker itself. Seems as if a utility library cannot be accessed?

Utterly defeated. I have spent days on this - no doubt reading the solution somewhere without having understood it. Any contributions (including 'rude but informative') gratefully accepted. I fully appreciate the necessity of mercilessly punishing newbies.

도움이 되었습니까?

해결책

Neither of those examples will work until you fix the call to the Array.sort method

Syntax

array.sort([compareFunction])

Javascript

function a() {
  var xyzzy = [40, 1, 5, 200];
  xyzzy.sort(); // notice the difference

  console.log(xyzzy);
};

a();

On jsfiddle

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