- MooTools 1.2 Beginner's Guide
- Jacob Gube Garrick Cheung
- 341字
- 2025-03-31 06:30:57
Chapter 2. Writing JavaScript with MooTools
Using MooTools means you'll be coding in a web application framework that excels in object-oriented programming and JavaScript best practices. Modern development principles, such as unobtrusive JavaScript programming and object-based logic is the bread and butter of MooTools.
For example, modern development practices dictate that we separate our site's functional and behavioral layers (server-side and client-side scripts) from our site's content structural layer (HTML markup). This concept is known as unobtrusive JavaScript, and we'll delve into this best practice here in this chapter. You'll see one of the more important ways of how MooTools makes us write better JavaScript.
But we're going to be covering more than just unobtrusive JavaScript. We're going to work with classes, which is the design pattern of the entire MooTools framework.
MooTools is structured into classes. In programming, a class is simply a template of an object. A class can contain many methods or properties such as normal variables or even references to other classes and objects.
To understand how to write great MooTools code that adheres to best practices, you have to understand its fundamental structure and the concept of classes, which I must admit, can be confusing and hard to grok.
If you know another programming language, especially an object-oriented one, or a pseudo-object-oriented one like PHP (this is where I learned the concept of a class), then the stuff we'll discuss here will look very familiar to you. You can then compare and contrast the difference in syntax of classes in your particular language. As an example, for you PHP folks out there, in PHP 5, we have the
__construct()
function, which you'll soon see, is exactly like theinitialize
method of a MooTools class.
In this chapter we shall:
- Learn how MooTools helps us write unobtrusive JavaScript
- Work with classes by creating our own class
- See how MooTools works by exploring a couple of classes
- Learn about the concept of chainability by working through a simple animation example
So let's get on with it!