The Object Slinger

The more JavaScript code I write, the less secure I am in thinking I have a handle on the language. Just when I settle on a way of doing something, a startlingly better way occurs to me.

JavaScript as Sexy Forest Nymph

JavaScript is a language that seems to have grown up without the sun shining on it. While Java is the bleach blonde fake-boob lifeguard chick surrounded by gawkers, JavaScript is a hauntingly mysterious gal in the forest that you can nearly catch a glimpse of as you scramble through blackberries and brambles.

Everyone seems to know the proper way to program in Java. It’s the language you learn in college. It’s formal. It’s strict. It’s how your mother would want you to code.

No one really seems to be sure of the “right way” to code in JavaScript. Or, rather, there are a lot of people who are sue how to program in JavaScript, but they don’t all agree with each other. It’s still the wild west.

When people start coding in JavaScript, many complain that it’s not like Java. Looking for Java in JavaScript is looking for heartbreak. Instead, treat JavaScript as a magical version of C that allows you to stuff objects and functions into variables. Sometimes you don’t even have to stuff them into variables–-you can create them on the fly and pass them around.

Maybe if C had been more like JavaScript we never would have moved to C++.


I remember that when I programmed in C, I’d often get into a situation where I had a function that had been returning one value and now I wanted it to return two values. I felt painted into a corner.

Sometimes I’d hack it so both values got returned together. If I knew x was in the range 0-15, and y was in the range 0-15, I’d return x*16+y and the calling function would do the math to break the values out into two pieces. In more difficult situations, I’d have to make a struct and pass a pointer to that around. Doesn’t sound like a big deal, really, but it broke the flow and made everything ugly.

In C++ or Java, you’re typically thinking about classes the whole way through. It’s a mindset where you’re not mad at having to put together a struct to hold structured data, you’re resigned to thinking in classes. OK, stop staring at the nymph and pay attention. I’m going to show you some code.

It’s Different

JavaScript is different. I don’t have to hack around like I did in C, and I don’t have to think in classes. I just return an object, and I can sling that object into existence right at the last minute, right in the return statement.

Here’s a ridiculous function that always returns an object that holds the numbers 10 and 12. A better example (and a rather common one) would be a library that does complex math. For illustration, this’ll do.

function giveMeX10Y12() {
    return {x:10,y:12};

The caller can easily pull the pieces apart:

console.log (obj.x,obj.y);

To me, this is what JavaScript programming is all about. It’s not about classes-–it’s about objects. It’s lightweight and comfortable, once you unlearn other languages and gain JavaScript chops.

Spaghetti Western

When you start programming JavaScript, you’re going to write some big plates of spaghetti. You’ll know you’re doing it badly. But learn to think in JavaScript and your code will be clean and easy to debug.


  1. rhettanderson said,

    June 28, 2008 at 5:06 pm

    Shout out to the person who found this message via a search for “sexy”. Hope you found what you were looking for.

  2. July 7, 2008 at 5:32 pm

    […] are quoting from The Object Slinger post. Who knew I had such a flair for writing in Spanish? […]

  3. June 4, 2010 at 5:01 pm

    This is such an interesting post. I’m in the midst of learning JS, but I started off as a web designer, with no formal computer science, or programming training. It’s been a bit tough to get up to speed, but now I feel like I’m starting to see it for what it is, and I’m almost glad I never learned C or Java. A few of my developer buddies get frustrated when using JS all the time, and a lot of the old code i encounter looks like folks are trying (their best) to shape JS into a classical language, when there always seems to be a better, or cleaner, or more Javascripty way of doing it that gives significantly less headaches.

    Great post; love the insight 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: