Live data from Hacker News

Advanced Javascript tips and tricks

code.google.com

21–30 of 37 posts

Re: Advanced Javascript tips and tricks

#22
post #5

BTW I also just noticed Resig's next advanced JS book is almost done (finally!) http://www.manning.com/resig/ 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.

I'm not sure if it's "almost done". I bought the MEAP (early access) a long time ago, and for months nothing happened at all. Then a second author came on board to get things moving. 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 versi…

Ha ha, yep I bought the MEAP version of this book almost three years ago. Can't believe it's not finished yet.

Re: Advanced Javascript tips and tricks

#25
I use the pattern listed under "Objects private and public members" frequently and feel they missed an important point.

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.

Re: Advanced Javascript tips and tricks

#26
post #7

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 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.

You don't understant. The code cited isn't even guaranteed to terminate by the spec; browsers could just go into an infinite loop if the comparison function effectively lies to them so the array never looks sorted... and this would be perfectly reasonable behavior.

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.

Re: Advanced Javascript tips and tricks

#27
post #6
post #3

Wow, 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!

Just note the small print that yield is JS 1.7 Firefox 2 was the first to have JS 1.7 but I dunno if any version of IE has yield, maybe IE9 ?

Well, that significantly dampens my excitement...

Re: Advanced Javascript tips and tricks

#29

I find some of them horribly obscure +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?

Your browser likely has a javascript console, it's pretty useful:

  +new Date()
  1309882329679
  +new Date()
  1309882331551
  +new Date()
  1309882332511

Re: Advanced Javascript tips and tricks

#30
post #22

Earlier quoted context omitted.

I'm not sure if it's "almost done". I bought the MEAP (early access) a long time ago, and for months nothing happened at all. Then a second author came on board to get things moving. 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 versi…

Ha ha, yep I bought the MEAP version of this book almost three years ago. Can't believe it's not finished yet.

inb4 duke nuken forever jokes
Post reply on HN