Book Image

Learning JavaScript Robotics

By : Kassandra Perch
Book Image

Learning JavaScript Robotics

By: Kassandra Perch

Overview of this book

Table of Contents (16 chapters)
Learning JavaScript Robotics
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

How a Johnny-Five program works


In this section, we'll take a look at the internals of a Johnny-Five program in more detail, so we can start building more complex applications.

Objects, functions, and events

Johnny-Five programs work using an event-based structure, and there are three concepts to keep in mind: objects, functions, and events.

Objects tend to programmatically represent our physical electronic components and are usually constructed using the new keyword. A few examples of objects in Johnny-Five include the five object, which represents the Johnny-Five library, the Board object, which represents our microcontroller; and the LED object, which will programmatically represent our LED:

var led = new five.Led(11);

Functions are available for the objects we create and usually represent the actions that our robots can do. For instance, the LED object has an on() and off() function that turns the LED on and off:

led.on();
led.off();
led.blink();
led.stop();

Events are fired on objects and...