Advanced Javascript tips and tricks
code.google.com
code.google.com
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.
So it's not that you get a bad random shuffle; it's that your code might never return from that sort() call.
And to be clear, there have been cases of "hang" bugs being reported to browser vendors in situations exactly like this one because people were writing boneheaded script like this.
list.map(function (t) { return [Math.random(), t]; }).sort().map(function (t) { return t[1]; })Here's a good writeup with a bunch of test runs and graphs: http://www.robweir.com/blog/2010/02/microsoft-random-browser...
yield
Optional named function arguments
let (in the 'remove an object from an array' example)
"Convert a string into a charcode list" (both methods)
toSource method on an array ("Array iteration pitfall")
etc.
and 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.
A few days ago I got an update from Manning:
"What's new?
Chapter 2, "Testing and Debugging" has been added.
Chapter 3, "Functions are fundamental" has been revised.
What's next?
Our next update will be coming quickly, with revised versions of Chapter 4 on closures and Chapter 5 on object-orientation with prototypes.
I haven't bothered yet to actually look at the PDF, it sounds like it's still in a rather early stage.Firefox 2 was the first to have JS 1.7 but I dunno if any version of IE has yield, maybe IE9 ?
Some of those extensions (e.g. Array Extras) trickled into the wider language, but as far as I know most of them (including let and generators) are confined to Gecko-based browsers still.
http://code.google.com/p/jslibs/wiki/JavascriptTips#delete_a...
They really should add something like:
function MyConstructor( pub, priv ) {
var thisMyConstructor = this; //**Copy the 'this' variable**
var privateVariable = priv;
this.publicVariable = pub;
Otherwise "public" variables can't be accessed in private functions because "this" no longer references the object. So, from what I've learned it's always best to copy "this" to a private variable and use the private variable for consistency.Isn't that what the concat() method is for?
+new Date() // 1259359833574
Oh come on, magic constants? How do you check if that's the right number? function PrefixInteger(num, length) {
return (num / Math.pow(10, length)).toFixed(length).substr(2);
}
Is there no sprintf in Javascript? Or did I misunderstand what the function does?Title: Milliseconds since epoch
+new Date()
1309882329679
+new Date()
1309882331551
+new Date()
1309882332511 Number(new Date())
or more simply Date.now()
if you don't need IE8 support.