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++.

Stuck

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:

obj=giveMeX10Y12();
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.

Advertisements