BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Pro JavaScript with MooTools Pro JavaScript with MooTools is unlike any other JavaScript book on the mar- ket today. While similar books focus on either JavaScript as a language of the browser or how to use JavaScript frameworks, Pro JavaScript with MooTools fills the gap between these topics and moves beyond—exploring the advanced features of JavaScript and how the MooTools framework uses these features to further improve the language itself. The book itself takes a unique three-pronged approach. It first walks you through the advanced features of JavaScript and the MooTools framework, including native augmentation and type creation, a comprehensive discussion of JavaScript functions, object-oriented programming with native JavaScript and MooTools Classes, and the MooTools Class internals. You’ll then learn all about JavaScript in the DOM: the Elements Class and its internals, the MooTools Event system, Selector engines and MooTools Slick, Ajax and the Request Object, and Animation and the Fx classes. The final section really sets the book apart from all others, as it discusses JavaScript outside the browser. You’ll take an in-depth look at CommonJS and MooTools, using MooTools outside the browser using Meso, and creating CommonJS web applications via the MooTools Deck framework. Mark Joseph Obcena US $44.99 Shelve in: Web Development / JavaScript User level: Intermediate–Advanced THE APRESS ROADMAP Pro JavaScript Design Patterns Pro JavaScript with MooTools MooTools Essentials Practical Prototype and script.aculo.us Beginning JavaScript with DOM Scripting www.apress.com SOURCE CODE ONLINE Companion eBook Obcena JavaScript with MooTools Companion eBook Available Pro THE EXPERT’S VOICE ® IN WEB DEVELOPMENT Pro JavaScript with MooTools Learning Advanced JavaScript Programming Mark Joseph Obcena Foreword by Valerio Proietti, MooTools Founder and Lead Developer Extending the Power of JavaScript Beyond the Browser by Using MooTools Written for MooTools 1.3 Download from www.eBookTM.com Download from www.eBookTM.com i Pro JavaScript with MooTools Learning Advanced JavaScript Programming ■ ■ ■ Mark Joseph Obcena Download from www.eBookTM.com ii Pro JavaScript with MooTools: Learning Advanced JavaScript Programming Copyright © 2010 by Mark Joseph A. Obcena All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3054-0 ISBN-13 (electronic): 978-1-4302-3055-7 Printed and bound in the United States of America (POD) Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editors: Frank Pohlmann and Ben Renow-Clarke Technical Reviewer: Simo Kinnunen Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editor: Sharon Terdeman Compositor: MacPS, LLC Indexer: Julie Grady Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. Download from www.eBookTM.com iii To the one who owns my heart. Download from www.eBookTM.com iv Contents at a Glance ■Contents v ■Foreword xiv ■About the Author xv ■About the Technical Reviewer xvi ■Acknowledgments xvii ■Preface xviii Part I: Exploring JavaScript and MooTools 1 ■Chapter 1: JavaScript and MooTools 3 ■Chapter 2: Functions 11 ■Chapter 3: Objects 47 ■Chapter 4: Classes 71 ■Chapter 5: Classes: The Extras 99 ■Chapter 6: Types 127 Part II: Conquering the Client Side 171 ■Chapter 7: JavaScript in a Window 173 ■Chapter 8: Elements 187 ■Chapter 9: Selector Engines 245 ■Chapter 10: Events 261 ■Chapter 11: Request 307 ■Chapter 12: Animation 339 Part III: Breaking from the Browser 359 ■Chapter 13: JavaScript without Limits 361 ■Chapter 14: MooTools on Deck 371 ■Appendix: Resources 387 ■Index 389 Download from www.eBookTM.com v Contents ■ Contents at a Glance iv ■Foreword xiv ■About the Author xv ■About the Technical Reviewer xvi ■Acknowledgments xvii ■Preface xviii Part I: Exploring JavaScript and MooTools 1 ■Chapter 1: JavaScript and MooTools 3 JavaScript 3 MooTools 4 The Language Extension 5 Your Toolkit 5 JavaScript Interpreter 6 JavaScript Console 6 MooTools Core and the Snippet Runner 8 Code Editor and JSLint 9 The Wrap Up 10 ■Chapter 2: Functions 11 The Function 11 One Function, Multiple Forms 12 Function Declaration 12 Function Expression 14 Named Function Expression 15 Download from www.eBookTM.com ■ CONTENTS vi Single-Execution Function 17 Function Object 19 Arguments 20 Return Values 24 Function Internals 25 Executable Code and Execution Contexts 25 Variables and Variable Instantiation 27 Scoping and the Scope Chain 29 Closures 31 The “this” Keyword 33 Advanced Function Techniques 36 Limiting Scope 36 Currying 38 Decoration 39 Combination 41 MooTools and Functions 42 Function Binding 43 Extending Functions with Methods 44 The Wrap Up 46 ■Chapter 3: Objects 47 JavaScript is Prototypal(-ish) 47 A Language of Objects 48 The Building Blocks of Objects 50 Constructor Functions 50 Prototypes 53 Inheritance 56 The Prototype Chain 61 Deliberate Chains 63 Simplified Prototypal Programming 65 Download from www.eBookTM.com ■ CONTENTS vii The Wrap Up 68 ■Chapter 4: Classes 71 From Prototypes to Classes 71 The MooTools Class System 73 Constructors and Initializers 75 Rethinking Members 77 Rethinking Methods 78 Rethinking Properties 80 Inheritance 83 Overridden Methods 86 Inside this.parent() 90 Mixins 91 The Wrap Up 96 ■Chapter 5: Classes: The Extras 99 Mutators 99 Implementing Your Own Mutators 101 Mutator Gotchas 104 The Built-in Mixins 106 The Chain Mixin 106 The Events Mixin 109 The Options Mixin 112 Static Members 114 Encapsulation and Visibility 118 Private Methods 120 Protected Methods 121 MooTools and Classes 125 The Wrap-Up 126 ■Chapter 6: Types 127 Values and Type Systems 127 Download from www.eBookTM.com ■ CONTENTS viii An Alternative Type System 129 Native Types and Values 131 Null and Undefined 131 Primitive Types 132 Composite Types 135 Type Casting 143 Casting Using Constructors 144 Casting Using Native Functions and Idioms 145 The MooTools Type System 147 The Type Constructor and Function Subclassing 147 Instance Checking 148 Type Detection 151 Working with Type Objects 152 Implementing New Members 153 Aliases and Mirroring 155 The extend Method and Generics 158 Creating New Types 160 A Table Type 160 The Table Constructor 162 Setter, Getter, and Removal 163 Membership Methods 164 Keys, Values and Traversals 165 Our Final Type 167 The Wrap-Up 168 Part II: Conquering the Client Side 171 ■Chapter 7: JavaScript in a Window 173 A Language for Every Computer 173 Life Cycle of a Page 174 Pause, Script 175 Download from www.eBookTM.com [...]... Developers using MooTools are expected to be JavaScript programmers first, MooTools users second In the MooTools community, a lot of emphasis is given to learning how to properly write JavaScript, and everyone is expected to know at least the basics of programming This is because MooTools itself is so intertwined with JavaScript that learning the framework without having a basic grasp of the language... making JavaScript truly an all-around programming language MooTools During the height of the Ajax period, JavaScript developers realized that some of the programming tasks they were doing were repetitive and could be simplified, so developers started releasing frameworks and libraries that included utility functions for common tasks as well as other helper methods and objects that simplified JavaScript programming. .. writing awesome JavaScript and MooTools material for years, in articles (like his “Up the Moo herd” series) and in actual code (Raccoon!) His blog is a mustread for anyone wanting to learn or expand his knowledge of MooTools, or JavaScript in general Pro JavaScript with MooTools isn’t simply a well-written technical book This book thoroughly explains how object-oriented programming works in JavaScript, ... some you can easily download for free So without further ado, here are the components of a JavaScript developer’s toolkit Download from www.eBookTM.com 5 CHAPTER 1 ■ JAVASCRIPT AND MOOTOOLS JavaScript Interpreter A JavaScript interpreter (or engine) is a program that parses and executes JavaScript programs You’ll need an interpreter that implements at least JavaScript version 1.5, which is equivalent... who proudly proclaim to be JavaScript developers don’t take time to learn the language enough to appreciate these quirks and features A big part of this problem, surprisingly, comes from the popularity of frameworks JavaScript s almost prodigal-son-like comeback into the limelight of web development has brought forth a slew of libraries and frameworks that promise an easier experience when working with. .. fully approve, by the way—but most programming languages behave more predictably Some languages behave predictably well, some languages behave predictably well with some exceptions, and some languages behave predictably weird The fascinating thing, though, is that a language’s predictability often has less to do with the language itself and more to do with its users The more we learn about a programming. .. Download from www.eBookTM.com 2 CHAPTER 1 ■■■ JavaScript and MooTools Before we begin our exploration of how to get the most out of JavaScript using the powerful MooTools framework, let’s focus a little on JavaScript and MooTools individually We’ll also talk about the JavaScript developer’s toolkit, and what you need to develop applications in JavaScript JavaScript JavaScript started out as a small language... best and most powerful JavaScript frameworks around The Language Extension MooTools complements JavaScript in a way that no other library could When people refer to MooTools, they call it a JavaScript library or a JavaScript framework Though these descriptions aren’t necessarily wrong, they don’t encapsulate the main idea behind MooTools—which is to leverage and improve the JavaScript language itself... was to bundle it with Netscape’s Navigator browser Before the browser’s release, though, Netscape acquired a trademark license from Sun Microsystems to use the name “Java” and the language was renamed to JavaScript This move was somewhat political on Netscape’s part: the release of JavaScript coincided with the new Navigator support for Java, a popular programming language, and the name JavaScript was... use the name JavaScript throughout since it’s the more popular name Download from www.eBookTM.com 3 CHAPTER 1 ■ JAVASCRIPT AND MOOTOOLS While JavaScript became popular among web developers, the rest of the programming world dismissed the language as nothing more than a curiosity This was probably due to the fact that web design was still in its infancy, and most of the developers who used JavaScript . i Pro JavaScript with MooTools Learning Advanced JavaScript Programming ■ ■ ■ Mark Joseph Obcena Download from www.eBookTM.com ii Pro JavaScript with MooTools:. FOR PROFESSIONALS BY PROFESSIONALS ® Pro JavaScript with MooTools Pro JavaScript with MooTools is unlike any other JavaScript book on the mar- ket today. While similar books focus on either JavaScript. DEVELOPMENT Pro JavaScript with MooTools Learning Advanced JavaScript Programming Mark Joseph Obcena Foreword by Valerio Proietti, MooTools Founder and Lead Developer Extending the Power of JavaScript