Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…
| /* | |
| --- | |
| name: Function | |
| description: Contains Function Prototypes like create, bind, pass, and delay. | |
| license: MIT-style license. | |
| requires: Type | |
| provides: Function | |
| ... | |
| */ | |
| Function.extend({ | |
| attempt: function(){ | |
| for (var i = 0, l = arguments.length; i < l; i++){ | |
| try { | |
| return arguments[i](); | |
| } catch (e){} | |
| } | |
| return null; | |
| } | |
| }); | |
| Function.implement({ | |
| attempt: function(args, bind){ | |
| try { | |
| return this.apply(bind, Array.from(args)); | |
| } catch (e){} | |
| return null; | |
| }, | |
| /*<!ES5-bind>*/ | |
| bind: function(that){ | |
| var self = this, | |
| args = arguments.length > 1 ? Array.slice(arguments, 1) : null, | |
| F = function(){}; | |
| var bound = function(){ | |
| var context = that, length = arguments.length; | |
| if (this instanceof bound){ | |
| F.prototype = self.prototype; | |
| context = new F; | |
| } | |
| var result = (!args && !length) | |
| ? self.call(context) | |
| : self.apply(context, args && length ? args.concat(Array.slice(arguments)) : args || arguments); | |
| return context == that ? result : context; | |
| }; | |
| return bound; | |
| }, | |
| /*</!ES5-bind>*/ | |
| pass: function(args, bind){ | |
| var self = this; | |
| if (args != null) args = Array.from(args); | |
| return function(){ | |
| return self.apply(bind, args || arguments); | |
| }; | |
| }, | |
| delay: function(delay, bind, args){ | |
| return setTimeout(this.pass((args == null ? [] : args), bind), delay); | |
| }, | |
| periodical: function(periodical, bind, args){ | |
| return setInterval(this.pass((args == null ? [] : args), bind), periodical); | |
| } | |
| }); | |
| //<1.2compat> | |
| delete Function.prototype.bind; | |
| Function.implement({ | |
| create: function(options){ | |
| var self = this; | |
| options = options || {}; | |
| return function(event){ | |
| var args = options.arguments; | |
| args = (args != null) ? Array.from(args) : Array.slice(arguments, (options.event) ? 1 : 0); | |
| if (options.event) args = [event || window.event].extend(args); | |
| var returns = function(){ | |
| return self.apply(options.bind || null, args); | |
| }; | |
| if (options.delay) return setTimeout(returns, options.delay); | |
| if (options.periodical) return setInterval(returns, options.periodical); | |
| if (options.attempt) return Function.attempt(returns); | |
| return returns(); | |
| }; | |
| }, | |
| bind: function(bind, args){ | |
| var self = this; | |
| if (args != null) args = Array.from(args); | |
| return function(){ | |
| return self.apply(bind, args || arguments); | |
| }; | |
| }, | |
| bindWithEvent: function(bind, args){ | |
| var self = this; | |
| if (args != null) args = Array.from(args); | |
| return function(event){ | |
| return self.apply(bind, (args == null) ? arguments : [event].concat(args)); | |
| }; | |
| }, | |
| run: function(args, bind){ | |
| return this.apply(bind, Array.from(args)); | |
| } | |
| }); | |
| if (Object.create == Function.prototype.create) Object.create = null; | |
| var $try = Function.attempt; | |
| //</1.2compat> |