Ext JS - Learning Center

Manual:Utilities:Function

From Learn About the Ext JavaScript Library

Jump to: navigation, search
Summary: Function
Author: Ext Community
Published: Unkown
Ext Version: 1.1
Languages: en.png English cn.png Chinese fr.png French

In Javascript, functions are objects that can have methods. The Ext library provides extensions to the Function object which all built-in functions, and all functions that you define, will inherit. These extra methods provide a very convenient way to bind a function to a certain scope.

Contents

The Importance of Scope

In JavaScript, you provide function references to be handlers in a similar manner to the way you provide function pointers in C. This means that there is no object bound to the function by default, and the this variable will be the browser's window object. This is the cause of much misunderstanding and many errors when beginning to write OO JavaScript. The following methods of Function allow you to bind an object to a function to be its scope (its this reference), and also to bind an argument list to a function.

Delegates and Callbacks

createDelegate

createDelegate (API reference) allows you to bind an object to a function with the object's scope, and to specify, as an array, the argument list that is to be passed to that function. Optionally, it takes a parameter which specifies where in the argument list to insert this array. If this third argument is not passed, the Array becomes the entire argument list.

Example:

var fn = func1.createDelegate(scope, [arg1,arg2], true)
fn(a,b,c) === scope.func1(a,b,c,arg1,arg2);
 
var fn = func1.createDelegate(scope, [arg1,arg2])
fn(a,b,c) === scope.func1(arg1,arg2);
 
var fn = func1.createDelegate(scope, [arg1,arg2], 1)
fn(a,b,c) === scope.func1(a,arg1,arg2,b,c);

createCallback

createCallback (API reference) allows you to bind an argument list to a function. Just specify the arguments as the parameters to createCallback.

Example:

var fn = func1.createCallback(arg1, arg2);
fn() === func1(arg1, arg2)

AOP Functions

The Ext library also provides some basic aspect-oriented programming (AOP) facilities. That means that a function of your choosing can be called before or after another function, and that it can alter the chain of execution depending on the outcome. This facility can be very useful when you want to extend the behavior of an Ext built-in function without using inheritance or overriding the Ext version. You can add your functionality either before or after the existing function using the following two methods.

createInterceptor

createInterceptor (API reference) allows you to specify a function that will be called before the function in question. It is passed all the arguments that the original function was passed. If it returns false, the original function will not be called. An optional scope parameter may be passed.

createSequence

createSequence (API reference) allows you to specify a function that will be called after the function in question. An optional scope parameter may be passed.

Deferred Execution

The Ext library also provides a convenient way of encapsulating the intrinsic JavaScript setTimeout call. The defer function (API reference) allows you to defer the execution of a function until a specified timeout has expired, but also allows you to specify both the scope in which the function will execute, as well as the argument list to pass to that function when it is called.

  • This page was last modified 16:32, 6 February 2008.
  • This page has been accessed 11,797 times.