Getting StartedGiving Commands to ObjectsPrograms 1Programs 2CalculatorVariables: Remembering StuffStrings 1Strings 2InputIfWhile: Loops and Repeating ThingsRandom Numbers and ChooseFSM: The Finite State Machine
Giving Commands to Objects

Someone giving commands to a dog

JavaScript is an object-oriented language. That means that the language has objects that you can give commands to. It's just like in real-life. If you have a dog, you can tell your dog to "sit" or to "roll-over."

Arrow pointing leftwards (possibly with the two robots too?) Or a zoom-out of the entire page?

If you look on the left, you will see two robots. The one on the left is called zbot because it has a big "Z" on it. The one on the right is called tvbot because its head is a giant tv. Zbot and tvbot are both objects. You can give commands to them.

diagram showing where to click

To give them commands, you first need to move your mouse inside the blue box of the Program Area and click.

diagram of actual UI with the command written there and a breakdown of the four components.

Then, you type whom you want to give a command to. After that, you type a period. Then, you type which command you want to give. And then, you type two round brackets and a semi-colon (a semi-colon is the symbol that looks like a comma with a dot over it). So, if you want zbot to wave its hand, you would write, "zbot.wave();" Make sure that all the letters are lower-case. JavaScript will get confused if you use capital letters.

Diagram showing person clicking on the Run button.

And lastly, you should click on the Run button.

Picture of a waving zbot?

Go ahead and try getting zbot to wave. Remember, the steps are 1. click inside the blue box of the Program Area, 2. type "zbot.wave();", and then 3. click on Run. Make sure you type everything exactly right.

Clicking on the Clear button, and then clicking on "Yes" when asked "Clear your program?"

Now, try getting tvbot to wave. First, click on the Clear button to get rid of the old command. Click on Yes when the computer asks you if you want to clear your program.

Picture showing tvbot.wave(); (possibly with zbot crossed out?)

Then, it's the same as before except instead of typing zbot, you type tvbot instead. (Tvbot has smaller arms, so it only waves a little bit.)

Examples of various commands you can give

Zbot and tvbot know how to do lots of things. Here are some of the things you can tell them to do: "blink," "clap," and "jump." Tvbot does not have any legs, so it cannot jump. Try out these different commands.

Example error? jump is not a function (bug image?)

If you give a command to one of the robots that it does not understand, don't worry. It is a bug, and JavaScript will complain and tell you what's wrong in the orange Messages area.

More than one command in the program area

You can also type more than one command in the Program Area.

A robot reading a list

The computer will follow the first command, and then do the next command, and then the next one, until it reaches the end. This list of instructions is called a "program."

|

Programming Basics

Programming Basics: Beginners

E-mail | About | Privacy Policy