dBlogIt by Dustin Boston

Vinyl

October 30, 2011 by Dustin Boston | 0 comments

image

I was just tak­ing a look through my father in-law’s awe­some record col­lec­tion. Omg. Van Halen, Led Zep­pelin, Fleet­wood Mac… I could go on and on. So much incred­i­ble clas­sic rock in one lit­tle egg crate.

First steps

October 22, 2011 by Dustin Boston | 0 comments

image

Jame­son took three lit­tle baby steps com­pletely on his own today! His bal­ance is great but he really doesn’t under­stand what he’s doing yet ;-)

Just started using JSCov­er­age for test­ing code cov­er­age in Javascript. It’s super sim­ple to implement—I even got it work­ing with RequireJS (just had to make sure it was using rel­a­tive URLs).

July 18, 2011
by Dustin Boston
0 comments

Javascript By Reference vs By Value

Some­times Javascript makes me want to kick lit­tle kit­tens. This time it’s because there is no built-in way to pass objects by value. First a lit­tle guide regard­ing Javascript and by ref­er­ence vs by value:

  • Prim­i­tive types (boolean and numeric) are passed by value
  • Objects, arrays, and func­tions are passed by reference
  • Strings are immutable so Javascript han­dles them internally

Why this matters

Every time you pass an object to a func­tion and change it, the orig­i­nal will also be mod­i­fied. This is under­stand­able behav­ior see­ing as the object could be quite large and there­fore memory-intensive if were to be copied at will. So how can you pass an object by value? Javascript: The Defin­i­tive Guide gives us a clue (empha­sis added):

A func­tion can use the ref­er­ence to mod­ify prop­er­ties of the object or ele­ments of the array. But if the func­tion over­writes the ref­er­ence with a ref­er­ence to a new object or array, that mod­i­fi­ca­tion is not vis­i­ble out­side of the function.

Basi­cally you have to cre­ate a new object and copy each of the orig­i­nal prop­er­ties to it. I have three things to say about this:

  1. It’s stu­pid, why can’t there just be a way to spec­ify by value like any other language?
  2. There’s no way around this lim­i­ta­tion. It’s built into Javascript on pur­pose by peo­ple much smarter than me.
  3. jQuery can help

jQuery to the rescue

jQuery has a nice method that can be used to copy one object to another: jQuery.extend(). Just add this line to the top of your func­tion when you do not intend to mod­ify the original:

var newObj = jQuery.extend({}, oldObj);

Make sure to check out the doc­u­men­ta­tion for more infor­ma­tion if you’ve got a com­plex object. Also, if you’re look­ing for a non-jQuery solu­tion, How to copy arrays and objects in Javascript pro­vides true object cloning (not copy­ing) which may make it more efficient.

July 14, 2011
by Dustin Boston
0 comments

Return false will break you

I just had this issue (and I’ve had it before) with jQuery live bind­ings not get­ting trig­gered. I checked and dou­ble checked. The ele­ment was in fact bound to the event. But when I clicked on it, noth­ing would hap­pen, and no errors were thrown. I spent a while telling Google about it but teh Googs had noth­ing to say. After a few hours I real­ized what I had done.

So what was the prob­lem? return false. There was another event bound to the ele­ment which had a return false at the end, thus pre­vent­ing sub­se­quent events from fir­ing. Once I removed it (and replaced it with event.preventDefault) every­thing worked as expected. Moral of the story is to be judi­cious when using return false.

For more infor­ma­tion read jQuery Events: Stop (Mis)Using Return False. Also, there was a really help­ful post/discussion over on Stack­Over­flow regard­ing when and how to use return prop­erly. Check it out.