![]() | Giving Commands to Objects |
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."
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.
To give them commands, you first need to move your mouse inside the blue box of the Program Area and click.
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.
And lastly, you should click on the Run button.
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.
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.
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.)
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.
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.
You can also type more than one command in the Program Area.
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."