MooTools
a compact javascript framework
MooTools Core v1.4.5
Core
Browser
Slick
Type: Function
函数对象的方法。另请参见:
Function: Function.from
如果传递的参数是一个函数,它会返回自身。否则,它会返回一个可以返回传递参数的函数。语法:
var foo = Function.from(obj);
参数:
- obj - (mixed)如果该参数是一个函数,它只会返回自身。否则,它会返回一个可以返回传递参数的函数。
返回:
- (function)返回一个函数,此函数返回传递的参数。
示例:
var fn = Function.from(42);
alert(fn()); // alerts '42'
var fn2 = Function.from(fn);
alert(fn2()); // alerts '42'
Function: Function.attempt
执行一系列函数,返回第一个非失败的函数,如果没有成功的将返回null。语法:
Function.attempt(fn[, fn, fn, fn, ...]);
参数:
- fn - (function)任意数量的函数。
返回:
- (mixed)第一个成功的函数返回值。
- (null)null,如果全部函数失败。
示例:
var result = Function.attempt(function(){
return some.made.up.object;
}, function(){
return jibberish.that.doesnt.exists;
}, function(){
return false;
});
//result is false
var failure, success;
Function.attempt(function(){
some.made.up.object = 'something';
success = true;
}, function(){
failure = true;
});
if (success) alert('yey!');
Function method: extend
扩展了一个函数新的方法或属性.语法:
myFunction.extend(key, value);
// Or
myFunction.extend(object);
参数:
- key - (string)方法或属性的
- value - (mixed)函数或属性值
- object - (object)包含多个属性或方法的对象
返回:
- (function)原函数
例如:
var myFunction = function(){};
myFunction.extend('alert', function(text){
alert(text);
});
myFunction.alert('Hello!'); // alerts Hello!
// Using objects
myFunction.extend({
alert: function(text){
alert(text);
}
});
Function method: implement
函数的原型扩展方法。语法:
myFunction.implement(key, value);
// Or
myFunction.implement(object);
参数:
- key - (string)方法或属性名.
- value - (mixed)函数或属性值.
- object - (object)包含新属性或方法的对象.
返回:
- (function)原函数.
例如:
var myFunction = function(){};
myFunction.implement('alert', function(text){
alert(text);
});
var myInstance = new myFunction();
myInstance.alert('Hello!'); // alerts Hello!
// Using objects
myInstance.implement({
alert: function(text){
alert(text);
}
});
注意事项:
implement和extend的区别,extend只影响一个对象的实例,而implement作用到原型上,影响此对象下的所有实例.Function method: attempt
试图执行函数,如果它不失败则返回此函数的返回值,否则返回null 。语法:
var myFunctionResult = myFunction.attempt(args[, bind]);
参数:
- args - (mixed)参数,或参数的数组。
- bind - (object, optional)函数绑定到此对象执行(函数内部this指向到此对象)。
返回:
- (mixed)这个函数的返回值。
- (null)函数失败。
示例:
var myFunction = function(){
return some.made.up.object;
};
myFunction.attempt(); // returns 'null'
var myFunction = function(val){
return val;
};
myFunction.attempt(false); // returns 'false'
Function method: pass
返回一个闭包。语法:
var newFunction = myFunction.pass([args[, bind]]);
参数:
- args - (mixed, optional)参数(如果传递多个参数,必须是一个数组)。
- bind - (object, optional)函数绑定到此对象执行(函数内部this指向到此对象)。
返回:
- (function)一个新函数,它的参数调用时被传递。
例如:
var myFunction = function(){
var result = 'Passed: ';
for (var i = 0, l = arguments.length; i < l; i++){
result += (arguments[i] + ' ');
}
return result;
}
var myHello = myFunction.pass('hello');
var myItems = myFunction.pass(['peach', 'apple', 'orange']);
// Later in the code, the functions can be executed:
alert(myHello()); // passes 'hello' to myFunction.
alert(myItems()); // passes the array of items to myFunction.
Function method: bind
改变this的作用范围,并向函数传递参数.语法:
myFunction.bind([bind[, arg1, arg2, ...]]);
参数:
- bind - (object, optional)函数绑定到此对象执行(函数内部this指向到此对象)。
- arg1, arg2, ... - (mixed, optional)参数传递给函数。如果被绑定的函数被调用时有其他参数,参数将被合并。
返回:
- (function)绑定后函数。
例如:
function myFunction(){
// Note that 'this' here refers to window, not an element.
// the function must be bound to the element we want to manipulate.
this.setStyle('color', 'red');
};
var myBoundFunction = myFunction.bind(myElement);
myBoundFunction(); // makes myElement's text red
// To show how bind works the following example:
var myBoundFunction = myFunction.bind(anyVar);
// is roughly equivalent with
var myBoundFunction = function(){
return myFunction.call(this);
};
Function method: delay
延迟执行一个函数。语法:
var timeoutID = myFunction.delay(delay[, bind[, args]]);
参数:
- delay - (number)等待时间(以毫秒为单位) 。
- bind - (object, optional)函数绑定到此对象执行(函数内部this指向到此对象)。
- args - (mixed, optional)传递的参数(如果参数多于一个,必须是一个数组) 。
返回:
- (number)JavaScript timeout id (用于清除延迟) 。
例如:
var myFunction = function(){ alert('moo! Element id is: ' + this.id); };
//wait 50 milliseconds, then call myFunction and bind myElement to it
myFunction.delay(50, myElement); // alerts: 'moo! Element id is: ... '
//an anonymous function which waits a second and then alerts
(function(){ alert('one second later...'); }).delay(1000);
//to stop the delay, clearTimeout can be used like so:
var timer = myFunction.delay(50);
clearTimeout(timer);
另请参见:
Function method: periodical
间隔执行的函数。可以使用clearInterval停止。语法:
var intervalID = myFunction.periodical(period[, bind[, args]]);
参数:
- period - (number)执行的间隔。
- bind - (object, optional)函数绑定到此对象执行(函数内部this指向到此对象)。
- args - (mixed, optional)传递的参数(如果参数多于一个,必须是一个数组) 。
返回:
(number)Interval id(用于清除间隔执行) 。例如:
var Site = { counter: 0 };
var addCount = function(){ this.counter++; };
addCount.periodical(1000, Site); //adds the number of seconds at the Site.
// the interval can be stopped using the clearInterval function
var timer = myFunction.periodical(1000);
clearInterval(timer);
copyright ©2006-2014 Valerio Proietti , translate by Jobin Sun