- moved every file from /Native to /Types
[mootools/dkf.git] / Source / Types / Function.js
blobd7a3bf7dd10a4e980739c49f9159eae86a6f698c
1 /*
2 ---
4 script: Function.js
6 description: Contains Function Prototypes like create, bind, pass, and delay.
8 license: MIT-style license.
10 requires: Type
12 provides: Function
14 ...
17 Function.extend({stab: function(){
18         for (var i = 0, l = arguments.length; i < l; i++){
19                 try {
20                         return arguments[i]();
21                 } catch (e){}
22         }
23         return null;
24 }});
26 Function.implement({
28         extend: function(properties){
29                 for (var property in properties) this[property] = properties[property];
30                 return this;
31         },
33         create: function(options){
34                 var self = this;
35                 options = options || {};
36                 return function(event){
37                         var args = options.arguments;
38                         args = (args != undefined) ? $splat(args) : Array.slice(arguments, (options.event) ? 1 : 0);
39                         if (options.event) args = [event || window.event].extend(args);
40                         var returns = function(){
41                                 return self.apply(options.bind || null, args);
42                         };
43                         if (options.delay) return setTimeout(returns, options.delay);
44                         if (options.periodical) return setInterval(returns, options.periodical);
45                         if (options.attempt) return $try(returns);
46                         return returns();
47                 };
48         },
50         run: function(args, bind){
51                 return this.apply(bind, $splat(args));
52         },
54         pass: function(args, bind){
55                 return this.create({bind: bind, arguments: args});
56         },
58         bind: function(bind, args){
59                 return this.create({bind: bind, arguments: args});
60         },
62         bindWithEvent: function(bind, args){
63                 return this.create({bind: bind, arguments: args, event: true});
64         },
66         attempt: function(args, bind){
67                 return this.create({bind: bind, arguments: args, attempt: true})();
68         },
70         delay: function(delay, bind, args){
71                 return this.create({bind: bind, arguments: args, delay: delay})();
72         },
74         periodical: function(periodical, bind, args){
75                 return this.create({bind: bind, arguments: args, periodical: periodical})();
76         }
78 });