Book Image

MooTools 1.3 Cookbook

By : Jay L Johnston
Book Image

MooTools 1.3 Cookbook

By: Jay L Johnston

Overview of this book

MooTools is a JavaScript framework that abstracts the JavaScript language. JavaScript itself, complex in syntax, provides the tools to write a layer of content interaction for each different browser. MooTools abstracts those individual, browser-specific layers to allow cross-browser scripting in an easy-to-read and easy-to-remember syntax. Animation and interaction, once the domain of Flash, are being taken by storm by the MooTools JavaScript framework, which can cause size, shape, color, and opacity to transition smoothly. Discover how to use AJAX to bring data to today's web page users who demand interactivity without clunky page refreshes. When searching for animation and interactivity solutions that work, MooTools 1.3 Cookbook has individual, reusable code examples that get you running fast! MooTools 1.3 Cookbook readies programmers to animate, perform AJAX, and attach event listeners in a simple format where each section provides a clear and cross-browser compatible sketch of how to solve a problem, whether reading from beginning to finish or browsing directly to a particular recipe solution. MooTools 1.3 Cookbook provides instant solutions to MooTools problems – whatever you want to do with MooTools, this book will tell you how to do it. MooTools 1.3 Cookbook is presented in a progressive order that builds concepts and ideas, while simultaneously being a collection of powerful individual, standalone, recipe solutions.
Table of Contents (17 chapters)
MooTools 1.3 Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Understanding MooTools ubiquitous anonymous functions


Functions have names, but anonymous functions do not; MooTools uses them everywhere.

Note

Anonymous functions are one-time use like disposable lighters. In MooTools, we use them to one-time define a method; reusable disposable functions!

Getting ready

To define and call an anonymous function raw JavaScript wraps a function in parentheses:

(function(){document.title=location.href;alert('done');})();

Nothing is returned, and the function is not assigned to a function identifier; when the () function syntax is appended, the function self-executes.

How to do it...

MooTools' use differs slightly in that we will pass this anonymous function as an argument to a MooTool object and bind it to an identifier; the anonymous syntax does not change.

<script type="text/javascript" src="mootools-1.3.0.js"></script>
</head>
<body>
<div id="my_trigger" style="width:100px; height:100px; border:1px solid #BEBEBE; line-height:50px; text-align:center;">Click Me</div>
<script type="text/javascript">
// (A) would change title, alerts done in true lisp style
//(function(){document.title=location.href;alert('done');})();
// (B) saves the anonymous function in a variable
var af = (function(){
document.title=location.href;
alert('done');
});
// (C) "calls" the anonymous function (commented out)
//af();
// (D) returns the function (and alerts its text)
//alert($lambda(af)); // note, lambda is deprecated
// (E) binds function to the click event on my_trigger
$('my_trigger').addEvent('click',
af
);
</script>

How it works...

  • A indicates a classical JavaScript style, anonymous, self-executing function.

  • B (used in our recipe) assigns a self-executing function to an identifier.

  • C in raw JavaScript would call the self-executing function.

  • D demonstrates deprecated usage of MooTools' $lambda object.

  • E (used in our recipe) passes the function to be bound to click events.

There's more...

Speaking in a strictly semantic world where anonymity is not compromised by that golden oldie 1984 and Orson Wells' cameras and dictatorial fiction, an anonymous function cannot be called more than once; it is defined, executed, and lost. Assigning it to an identifier would, in a pure discourse, render it no longer anonymous. Verily, the point of a recipe on anonymous functions is but to aid us in our understanding behind the science on how functions are passed to objects and then later executed as methods. Read up on more about what anonymous functions are on your favorite wiki site and send the author assertively argumentative proof as to the gamut of viewpoints regarding this subject.