Book Image

MooTools 1.2 Beginner's Guide

Book Image

MooTools 1.2 Beginner's Guide

Overview of this book

MooTools is a simple-to-use JavaScript library, ideal for people with basic JavaScript skills who want to elevate their web applications to a superior level. If you're a newcomer to MooTools looking to build dynamic, rich, and user-interactive web site applications this beginner's guide with its easy-to-follow step-by-step instructions is all you need to rapidly get to grips with MooTools.
Table of Contents (14 chapters)
MooTools 1.2 Beginner's Guide
Credits
About the Authors
About the Reviewer
Preface

Time for action—exploring the $each function


Let's see how the $each function helps us to list down our dream cars:

  1. Let's set up the structure of the $each function. As our target object, we pass the argument of myCars. priority a the current item value, and car the current object's key.

    $each(myCars, function(priority, car){
    // Code to print out each car goes here.
    });
    
  2. We'll use the .write() method to display the contents of myCars.

    $each(myCars, function(priority, car){
    document.write('<strong>Priority:</strong> '+priority+', <strong>Car name:</strong> '+car+'<br />');
    });
    
  3. Preview the script in your browser, and voila, you've got a list of my dream cars.

What just happened?

We explored a quicker way of iterating through objects using the $each function. $each can be used on any objects that are iterable, including arrays and class objects. The $each function saves you time in having to write complex, nested for and while loops.

Other utility functions in the Core...