MooTools framework "Four"-function: Main Method analysis

Source: Internet
Author: User
Tags bind call back closure mootools

Content Description:

Closures are a very powerful tool in JavaScript, and we basically use them in real-world application development. From each JavaScript frame, we can also see the shadow of the closure everywhere. MooTools has extended some very convenient methods of creating function closures on JavaScript functions, some of which we have used in prototype.js, such as Bind,bindaseventlistener.

With the extension of function, MooTools mainly provides a create method for creating closures, which in fact implements all the extensions of MooTools to function, but generally we don't use it directly, Instead, use the other methods derived by mootools based on this create method.

1. Method: Create

List of parameter options:

Bind: The object that this is pointing to in the closure function that is created. The default points to the current function.

Event: Default is false; If true, the closure function created will be used as an event supervisor

Listener, the first argument passed is an event object; If it is a class name, the

Creates an instance of this class and passes the event object to the class

Arguments: A list of parameters that are passed into the created closure function as an array. If you specify both the event option above and this arguments option, the Event object events object will be the first in the argument list, and the other arguments are followed.

Delay: The number of milliseconds to delay execution (the settimeout function). The default does not delay execution. If you specify

, execution is deferred by the specified number of milliseconds when the closed package function is called (call back

Back to a time processing handle).

Periodical: The number of milliseconds (SetInterval function) performed at intervals. No interval execution is performed by default.

If specified, the specified closure function is called at each interval for the millisecond

Triggers execution after a number (the call returns a time processing handle).

Attempt: If you specify true, the catch exception will be used in the process of creating the closure, as

If no exception is thrown, the normal closure function is returned, and if an exception occurs, the caught exception object is returned. The default is False.

Example:

<input type= "button" value= "Test" onclick= "B ()"/>
function MyClass () {
Alert (' X: ' + event.clientx);
}
function A () {
Alert (' Base ... ');
}
var B = a.create ({' event ': MyClass});
When the button is pressed, the alert appears first as "x:33", followed by alert
function A (P1,P2) {
Alert (This.gettime ());
Alert (p1+ ' | | | ') +P2);
}
var B = a.create ({' bind ': New Date (), ' arguments ': [100,200]});
b ();
Alert out as "1233445"
Alert Out "100| | 200 "

2. Method: Pass

Parameter list:

Args: Parameters passed in array form

Bind: Optional. This point to the object

Role: Create a simplified version of the function. Implementation is the CREATE option ' arguments ' function.

Example:

function A (P1,P2) {
Alert (p1+ ' | | | ') +P2);
}
var B = A.pass ([100,200]);
b ();

3. Method: Attempt

Parameter list:

Args: Parameters passed in array form

Bind: Optional. This point to the object

Function: Create features a simplified version, and the closure function is created after execution. The implementation is the Create

The option ' attempt ' is true when it is selected.

Example:myFunc.attempt([100,200]);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.