“I Object To Objects By Reference” or
“The Power Of jQuery Compels Me”

I’m working on a sexy piece of software at the moment. It’s for somebody else so I can’t share the details. What I can say is that I was tasked with creating a custom object-oriented MVC JavaScript framework. I’m deep in the heart of AJAX country and am really enjoying using JavaScript and jQuery.

I ran into a gotcha today when attempting to overwrite a JavaScript object with a blank one. Here is an example that highlights my issue…

As it turns out these lines of code were the offenders

These ‘copies’ of the aDeveloper object weren’t making copies at all…they were making a ‘shallow copy’ or a ‘copy by reference’ each time. This means that when I modified this.chris to say ‘I like monster movies’ I was also modifying the aDeveloper object.

In order to make a true copy (or ‘deep copy’), I struck down the offending code and wielded the power of jQuery.

jQuery to the rescue! Here’s the entire solution.

OK codeslingers, that’s it for now. Go make something great!