Be Dangerous: Just Enough JavaScript[2]

Objects in Space

If we backtrack to Just Enough JavaScript[0] and JEJ[1] you'll see that I am trying to very casually introduce people who may not be familiar with coding into the world of JavaScript. Of course some of that entails casually introducing people who may not be familiar with the world of coding itself. Coders talk in terms of variables and functions and methods and to really appreciate any language you need to be able to embrace that as part of your vernacular.

Last time we talked about functions, which is a way for group several things we want something to do under one command. We talked about variables, which are ways to track important pieces of information.

So functions are ways to do stuff and variables are the stuff itself.

Now here's the kicker, especially when it comes to JavaScript - they're both objects.

And this gets a little weird, especially if you think of objects as physical things in your room like a vase or vacuum cleaner (like a normal sane person would).

Because in JavaScript it can be a vase. Or a vacuum cleaner. And something that is a vase could be a vacuum cleaner with a single line of code.

To understand that, let's take a step back and go to one of the most fundamental aspects of JavaScript:

Everything is an object

So that function? It's an object. The variable the function uses to get its job done? It's an object. Everything is an object. However, unlike the vase you have in your house, an object in JavaScript can change definition at any time.

To get your head around this, I point to the excellent (and final) FireFly episode Objects In Space.

Because the nature of the episode is that anything can be anything else given the right situation. My man Jubal sums it up best:

Jubal Early: So is it still a room when it's empty? Does the room, the thing, have purpose? Or do we - what's the word?

Dr. Simon Tam: I really can't help you.

Jubal Early: The plan's to take your sister; get the reward, which is substantial - "imbue", that's the word.

Jubal, who is a contender for the character with the most fascinating lines of dialogue in the least amount of air time, is describing a kind of transmigration. River's room isn't really a River's room anymore because River isn't in it. Without her, it is not "imbued" with function. If we were to express this in code, it would be something like:

var riversRoom = new Room(Owner='River Tam');

riversRoom.exit('River Tam');

riversRoom = null;

First we construct River's room (more on that in a later post). When we construct it, we place River in it as the owner. Then we exit River and finally, using the all powerful "=" operator ... we make it null, which is a fancy programmer way of saying "non-existent".

It existed, it changed based on it's function, and based on that we didn't need it any more ... so we made it not exist.

This stuff happens all the time in code.

Now you might be asking yourself, "what about that weird thing your wrote about a vase becoming a vacuum cleaner?"

Which is a reasonable thing to ask. JavaScript is what is known as a "typeless" or "loosely typed" language. In a strictly typed language, objects are declared to be a very specific thing and they are always that specific thing while they exist. They become different versions of that thing - so maybe a vase becomes a fancy vase or a flower vase or a standing vase, but for the most part a vase is a vase.

You can usually tell a strongly typed language because the type is included with the variable name:

var Vase new_vase = new Vase(color='blue');

Would create a variable named new_vase which is defined as a vase and not a vacuum cleaner.

Not in JavaScript. Remember, in JavaScript:

Everything is an object

So even if you say an object is a room, you can tear it back down to a object and make it something else with that all powerful "=" operator.

River says it best in the episode:

River: Wrong about River. River's not on the ship. They didn't want her here, but she couldn't make herself to leave. So she melted. Melted away. They didn't know she could do that, but she did.

Jubal Early: I'm not sure I take your meaning there.

River: I'm not on the ship. I'm in the ship. I am the ship.

Simon: River...

River: River's gone.

Jubal Early: Then who exactly are we talking to?

River: You're talking to "Serenity." And Early, "Serenity" is very unhappy.

In code, this would look something like:

var River = new Character('River Tam');

River.melted();

River = new Spaceship('Serenity');

River.setMood('unhappy');

River was a character, but then she melted and became a ship. Any functions she had as a character are now gone and now she is a ship. As you can imagine, this is very powerful in JavaScript but can also lead to some pretty bizarre outcomes.

Next we will start putting all of this together to see how code really works. Because so far we have been talking about core structures (objects, variables, functions) but not the most important part: logic.

comments powered by Disqus