

Some good javascript tips & handy functions - jauco
http://code.google.com/p/jslibs/wiki/JavascriptTips

======
jauco
Generator expressions:

    
    
        [ y for ( y in [5,6,7,8,9] ) ] // is [0,1,2,3,4]
    

and

    
    
        [ y for each ( y in [5,6,7,8,9] ) ] // is [5,6,7,8,9]
    

Because in for extracts index names, and for each extracts the values.

~~~
tumult
Oh dude, you're the man. I didn't even know you could do comprehensions in JS.

~~~
tlrobinson
Of course it's not cross-browser, but if you're doing server-side JS it might
be useful.

~~~
mpk
It's not just server-side or client side these days. Using Rhino I'm using JS
for compressing JS files for production and generating documentation for said
JS files.

------
pmjordan
Regarding "shuffle the Array":

list = list.sort(function() Math.random() > 0.5 ? 1 : -1);

I'm wondering just how reliably random the result is. Does JavaScript make any
guarantee whether the comparison function will only be called once for a given
pair of values, and if not, could this affect the result?

~~~
jauco
Well, even if they compare multiple times the order will still be 'random' as
in not easily predictable

~~~
fh
On the contrary, I bet that depending on the exact search algorithm, it would
be possible to make some very specific predictions. To pick a simple example,
say you use this technique on quicksort of the "pivot on the first element"
variety. Given the nature of the algorithm, the first source element will end
up near the center of the "randomized" list with high probability, not at all
what you'd expect from an unbiased random permutation.

Granted, no real world sort is that simple, and it's even possible that the
"clever technique" will produce an unbiased permutation for _some_ sorting
algorithms. But why waste time thinking about this when the Knuth shuffle, a
proper way to randomly permute a list, only takes a few lines of code more and
is so well-documented?

------
soubok
Latest Mozilla JavaScript has also Generators (yield keyword), Destructuring
assignment (var [a,b,c] = [1,2,3]) and just-in-time compilation.

------
TweedHeads
The more I read the less I know

