Objects in Space
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.
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).
Everything is an object
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 = 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?"
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.
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.
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 = new Spaceship('Serenity');
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.