Live data from Hacker News

Advanced Javascript tips and tricks

code.google.com

11–20 of 37 posts

Re: Advanced Javascript tips and tricks

#11
post #9

Earlier quoted context omitted.

I think there's also an issue of performance -- it's possible, though unlikely, that the sort could take forever to complete.

Unlikely is an understatement -- the probability of an infinite sequence of PRNG outputs that prevents any sort algorithm from terminating is negligible.

What about 'taking too much time' though?

Re: Advanced Javascript tips and tricks

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

Re: Advanced Javascript tips and tricks

#13

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…

Fisher-Yates is a better idea, of course, since it is pretty short and runs in linear time, but if someone insists on a sort-based one-linear this should work: list.map(function (t) { return [Math.random(), t]; }).sort().map(function (t) { return t[1]; })

Fisher-Yates is also in-place, just like Array.prototype.sort.

Re: Advanced Javascript tips and tricks

#14
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 ?

No version of IE or Safari or Chrome has yield.

Re: Advanced Javascript tips and tricks

#15
Lots of the stuff in this list is only available in Mozilla browsers. It's not part of the ECMAScript standard:

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.

Re: Advanced Javascript tips and tricks

#17
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 ?

Javascript 1.x where x > 5 are proprietary extensions of Mozilla.

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.

Re: Advanced Javascript tips and tricks

#18
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?

Re: Advanced Javascript tips and tricks

#19

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?

> +new Date() // ...

Title: Milliseconds since epoch

Re: Advanced Javascript tips and tricks

#20

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…

Fisher-Yates is a better idea, of course, since it is pretty short and runs in linear time, but if someone insists on a sort-based one-linear this should work: list.map(function (t) { return [Math.random(), t]; }).sort().map(function (t) { return t[1]; })

Argh, I missed the edit time window: "one-linear", should be, of course, "one-liner". Fisher-Yates is O(n), but the sort makes this algorithm Omega(n log n).
Post reply on HN