Advanced Javascript tips and tricks
code.google.com
Advanced Javascript tips and tricks
1–10 of 37 posts
Re: Advanced Javascript tips and tricks
#2http://code.google.com/p/jslibs/wiki/JavascriptTips#delete_a...
Re: Advanced Javascript tips and tricks
#3Re: Advanced Javascript tips and tricks
#4 list = list.sort(function() Math.random() - 0.5);
Please don't shuffle an array like this. Array.prototype.sort requires the comparison function to be referentially transparent (i.e. to always return the same result when given the same two elements to compare), otherwise the sort order is undefined. For example, imagine how far from "shuffled" the resulting array will be if a bubble sort is used with a randomised comparison function.Use a Fisher-Yates shuffle instead.
Re: Advanced Javascript tips and tricks
#5and there is a 50% off code that is working for July 4th - july450
which makes it $16 for the early-edition PDF-only version
(or $20+$5 shipping for dead-tree version but it's $25 on amazon anyway)
If I learn just one new trick for $16 it's probably worth it.
Re: Advanced Javascript tips and tricks
#6Wow, I'm seriously overjoyed to discover the "yield" operator, which I did not know existed (it barely shows up anywhere!). No more hackishly using window.setTimeout() to yield in JS pseudo-thread simulations!
Firefox 2 was the first to have JS 1.7 but I dunno if any version of IE has yield, maybe IE9 ?
Re: Advanced Javascript tips and tricks
#7list = list.sort(function() Math.random() - 0.5); Please don't shuffle an array like this. Array.prototype.sort requires the comparison function to be referentially transparent (i.e. to always return the same result when given the same two elements to compare), otherwise the sort order is undefined. For example, imagine how far from "shuffled" the resulting array will be if a bubble sort is used with a randomised com…
Re: Advanced Javascript tips and tricks
#8list = list.sort(function() Math.random() - 0.5); Please don't shuffle an array like this. Array.prototype.sort requires the comparison function to be referentially transparent (i.e. to always return the same result when given the same two elements to compare), otherwise the sort order is undefined. For example, imagine how far from "shuffled" the resulting array will be if a bubble sort is used with a randomised com…
While I agree in principle, there are many cases where you don't care about the statistical quality of a random shuffle, where brevity has its benefit.
Re: Advanced Javascript tips and tricks
#9Earlier quoted context omitted.
While I agree in principle, there are many cases where you don't care about the statistical quality of a random shuffle, where brevity has its benefit.
I think there's also an issue of performance -- it's possible, though unlikely, that the sort could take forever to complete.
Re: Advanced Javascript tips and tricks
#10list = list.sort(function() Math.random() - 0.5); Please don't shuffle an array like this. Array.prototype.sort requires the comparison function to be referentially transparent (i.e. to always return the same result when given the same two elements to compare), otherwise the sort order is undefined. For example, imagine how far from "shuffled" the resulting array will be if a bubble sort is used with a randomised com…
list.map(function (t) { return [Math.random(), t]; }).sort().map(function (t) { return t[1]; })