Question

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.

Was it helpful?

Solution

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

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