r/javascript • • May 02 '10

31 jQuery Snippets That Will Help Make You A JavaScript Pro

http://addyosmani.com/blog/31-jquery-snippets/
5 Upvotes

12 comments sorted by

19

u/nostrademons May 02 '10

Uh, copying snippets off a webpage will never make you a "JavaScript Pro". Learning to understand JavaScript at a fundamental level will.

These are nifty snippets, but some truth in headlines here, plz...

4

u/[deleted] May 03 '10

I cringed and had the same general thought.

This kind of dreck is embarrassing. "Pro"...jesus.

-1

u/schnalle May 05 '10

understanding javascript at a fundamental level will most likely make you insane.

function foo() {                     function bar() {
    var f = function() 42;               function f() 4711;
    function f() 4711;                   var f = function() 42;
    return f;                            return f;
}                                    }

now, what is

foo()() === bar()()

0

u/[deleted] May 19 '10

[deleted]

1

u/schnalle May 20 '10

yeah, and who needs functions that just return 42 or 4711 anyway?

2

u/dieomesieptoch May 03 '10

Just a minute ago I was checking some image gallery demo where the code listings were headed: "html", "css" and "jquery", which was funny on it's own but "snippets that will make you a pro" takes the cake. Well done.

2

u/artificialidiot {♆} May 03 '10

Man, I... totally... fell like a pro now.

0

u/kristopolous May 02 '10 edited May 02 '10

parseJSON? Isn't that just eval - but without a try/catch so that it will just silently crash on error?

And what's with this "data" thing ... who the hell wants to strongly couple their data with specific UI elements?

3

u/nostrademons May 03 '10

parseJSON also prevents you from running arbitrary code, so if someone hijacks your connection they can't steal the users' cookies, submit to forms, and rewrite the page.

0

u/kristopolous May 03 '10 edited May 03 '10

That's a minuscule gain at most as the only threat model that has a non-zero gain from that class of scrutiny is exceedingly rare.

How would someone hijack a connection and be limited at the application layer? It doesn't work this way.

Sure, if your assumption is completely dynamic pages wherein all the code and layout is done upfront, I agree with you. But what about the other 99.999 9% of websites?

FB does latent code loading and a reload on a new tab, so you can't use that. Google Docs does the same.

1

u/masklinn May 03 '10

parseJSON? Isn't that just eval - but without a try/catch so that it will just silently crash on error?

No.

  1. JSON parsers generally do a bit of validation, that's important especially when the json comes from untrusted sources (and yes, json can come from untrusted sources) as it opens the door to code injection attacks.

  2. For a while now, some browsers have provided access to their native Javascript parsers for JSON data: Firefox >= 3.5, Webkit (Safari, Chrome), IE >=8, Opera >= 10.5, and libraries can delegate for much improved performances with no loss in security. That native interface is, in fact, part of ECMAScript 5.

  3. Silently crash on error? What?

And what's with this "data" thing ... who the hell wants to strongly couple their data with specific UI elements?

It's generally UI-related data, and should work correctly cross-browser (whereas localStorage isn't yet available everywhere, as far as I know). And it allows for data scoping, making data automatically disappear when the relevant application section goes away.

2

u/kristopolous May 03 '10 edited May 03 '10
  1. Example?

  2. The Function("params", "code") declaration has been around since Netscape 3. That's how the lovely jquery does it.

  3. Yes, .ajax, .get, .getjson no matter how you wrap it, it will crash if not well formed - and do so silently

It's generally UI-related data, and should work correctly cross-browser (whereas localStorage isn't yet available everywhere, as far as I know).

localstorage? You are talking about the 1M/domain, 256K/page storage of HTML5? There's cross browser equivalents; you can even do it in your <hype>almighty jquery</hype> on IE6

And what does that have to do with the non-persistent .data storage? If you really want to do $("#elementname").data instead of data.elementname then ok, you have your own silly syntax. Using it as an excuse to be sloppy with resource management is irresponsible.

0

u/masklinn May 03 '10

Example?

Example of what?

The Function("params", "code") declaration has been around since JS 1.2 which is Netscape 3. That's how the lovely jquery does it.

Not quite. It uses native implementations if available, and it falls back on Function if native isn't available. And it starts by validating that the input is actually json.

Yes, .ajax, .get, .getjson no matter how you wrap it, it will crash if not well formed - and do so silently

You aren't talking about json anymore here, you're talking about json within the context of a network request which is an entirely different situation. The part eating the error is the request/response handling code, jQuery.parseJSON does raise an exception in case of invalid JSON.