Posts Tagged ‘Javascript’

Way to throw down the gauntlet

Monday, March 1st, 2010

So, I’m innocently listening to the YayQuery podcast this morning, and some guy calls in questioning the use of the try/catch statement (no name). After he asks his question Rebecca Murphy makes the assertion that, “That was like our best yayQuery partyline question YET”. I was like, “aw man, she’s right…” but then Paul Irish, Adam J. Sontag and Alex Sexton (Who uses the exact same wordpress theme as me, so crap, now I have to change my theme again) all start back-peddling for her saying “No offense to Chris McCulloh” etc. Thanks guys. She’s right though, especially once she added the clarifying, “that was like, an in-depth question”. My questions were very short.

So, now I have to come up with a *good* question to ask. I’ve even got one brewing, but who knows if it will live up to Rebecca’s standards ;) . I shaved off the beard, so maybe I’ll even record it to video and send it in like I did the other ones…

new Array() vs [] in JavaScript

Wednesday, February 10th, 2010

Did a little research recently about the difference between creating an array in Javascript using the array literal, or the fully qualified “object” name (Array() vs []). Here is what I found:

  • Most experienced JS programmers use []. This is what I was told by Paul Irish. When I asked Paul what he thought of a JS book I was reading, he immediately looked for an array definition (which ended up being Array() instead of []) and pointed it out as being “old fashioned”. [] is the way Douglas Crockford teaches it in JavaScript the Good Parts (they even talk about it in JavaScript the Definitive Guide). Also, if you look in the unminified source for the jQuery library, you will never see Array().
  • They appear to be nearly identical, except they aren’t.
  • [] is more terse and readable, especially for nested arrays:

    (try @ pastebin.me)

    vs:

    (try @ pastebin.me)

  • Using the Array() keyword makes you think about arrays as being somehow different from Objects (which, yes they are in-so-much as they are Objects with actual methods already defined for you). The literal helps you think about them more object-like because [] looks like {}. Plus, [] is more consistent with the way that you then access the array something[0] so there is less to remember.
  • Most importantly, Array() doesn’t behave uniformly across argument numbers and types. ((new Array(X)).length === 1 for any X as long as typeof(X) != “number” else Array(X).length === X)

Removing and re-using hard-coded onclick JavaScript methods with jQuery

Wednesday, January 27th, 2010

Let’s say you’ve been tasked with the ominous task of taking a currently “working” checkout process written in a hodge-podge of Prototype, jQuery and plain jane JavaScript and adding click methods to all input and a elements. Now, let’s also say you can’t modify the current HTML/JavaScript at all (mostly because it’s all working and you don’t want to break it and it’s already indecipherable code). Now, let’s also say that the click method you are attaching needs to be hands down, no matter what, the FIRST method that executes when you click on the element.

Sure you can make your JavaScript the first JavaScript that gets run on the page (so that the dynamic bindings run first, and therefor (hopefully) execute first). But what about the HARD CODED html click methods? This little code snippet will go through and dynamically remove (and “cache” (save for later)) all of the hard-coded onclick functions, and then bind your live functions to do your new critical must trigger first event, and then call the original onclick method on the element.

Yes, this code is scary. Yes, I cried a little after I wrote it while curled up in a ball under my desk. Yes, I plan to submit it to thedailywtf.com. But sometimes you just have to do what you have to do. If you’re in a similar situation, this might just help you out a little. Prepare to throw up in your mouth, hold on to your hats, and dive in:

To understand this a little more, you might like to throw a whole bunch of console.log() statements in there to see the state of each variable at each step along the way. It’s actually kind of fascinating that this code even works.

The toString() on the onClick on the attr call actually returns:
"function onclick(e){doThing();}

This is scary/cool/weird since the attribute it’s pulling is just
<input type="checkbox" onclick="doThing()" />

So I go through with the regex and strip off the “ onclick” leaving just an anonymous function: “function(e){doThing();}

I then assign the anonymous function to the variable “clickMethod” thus creating a brand new function on the page called clickMethod, which can be triggered or called anywhere I want and even passed an event object!!!

*BARF* XP

Someone please tell me there’s a better way… If so, why not hook me up on FIXEE???