Javascript is a flexible lanaguage and it allows the programmer to add functions to existing base objects within Javascript. The reason for doing this is because there are a number of functions that are extremely useful to use on the base object, however they are either not implemented at all, or only implemented in certain browsers. When using multiple libraries, they may both implement functions on the base javascript objects. This overlap has the potential to cause conflicts when using these libraries together. For this reason, Ext adds only a few necessary functions onto the base objects. Here is an interesting comparison about different libraries and their javascript augmentation usage: Framework Scanner
The following functions have been added to the Function prototype (note that createSequence and createInterceptor have not been included):
var sayHello = function(firstName, lastName){
alert('Hello ' + firstName + ' ' + lastName);
};
Ext.get('myButton').on('click', sayHello.createCallback('John', 'Smith');
var sayHello = function(firstName, lastName, e){
alert('Hello ' + firstName + ' ' + lastName);
};
Ext.get('myButton').on(
'click',
sayHello.createDelegate(this, ['John', 'Smith'],
//0 indicates we want to insert our arguments before any others.
0
);
var whatsTheTime = function(){
alert(new Date());
};
whatsTheTime.defer(3000); //Wait 3 seconds before executing.
The following methods are added to the Array prototype, if and only if they are not already implemented by the browser:
var idx = [1, 2, 3, 4, 5].indexOf(3); //Returns 2.
var arr = [1, 2, 3, 4];
arr.remove(2);
var len = arr.length; // len is now 3.
The String class has a single format method added. Note that it may conflict when using Ajax.NET.
var s = String.format( 'Hey {0} {1}', how are you?', 'John', 'Smith' ); //{0} is substituted with John, {1} is substituted with Smith.