From d55932fc8c60847406789fa57c998c6ec6c46385 Mon Sep 17 00:00:00 2001 From: Tobie Langel Date: Tue, 30 Sep 2008 10:12:54 +0200 Subject: [PATCH] function.js rewrite. --- src/lang/function.js | 111 +++++++++++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 41 deletions(-) diff --git a/src/lang/function.js b/src/lang/function.js index 6ce56fd..9237fb1 100644 --- a/src/lang/function.js +++ b/src/lang/function.js @@ -1,57 +1,86 @@ -Object.extend(Function.prototype, { - argumentNames: function() { +Object.extend(Function.prototype, (function() { + var slice = Array.prototype.slice; + + function toArray(args) { + return slice.call(args, 0); + } + + function combine(array, args) { + var arrayLength = array.length, length = args.length; + while (length--) array[arrayLength + length] = args[length]; + return array; + } + + function argumentNames() { var names = this.toString().match(/^[\s\(]*function[^(]*\(([^\)]*)\)/)[1] .replace(/\s+/g, '').split(','); return names.length == 1 && !names[0] ? [] : names; - }, - - bind: function() { - if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this; - var __method = this, args = $A(arguments), object = args.shift(); - return function() { - return __method.apply(object, args.concat($A(arguments))); - } - }, - - bindAsEventListener: function() { - var __method = this, args = $A(arguments), object = args.shift(); - return function(event) { - return __method.apply(object, [event || window.event].concat(args)); - } - }, - - curry: function() { - if (!arguments.length) return this; - var __method = this, args = $A(arguments); - return function() { - return __method.apply(this, args.concat($A(arguments))); - } - }, + } - delay: function() { - var __method = this, args = $A(arguments), timeout = args.shift() * 1000; + function bind() { + if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this; + var __method = this, args = toArray(arguments), object = args.shift(); + return function() { + var combinedArgs = combine(args.clone(), arguments); + return __method.apply(object, combinedArgs); + } + } + + function bindAsEventListener() { + var __method = this, args = toArray(arguments), object = args.shift(); + return function(event) { + var combinedArgs = combine([event || window.event], args); + return __method.apply(object, combinedArgs); + } + } + + function curry() { + if (!arguments.length) return this; + var __method = this, args = toArray(arguments); + return function() { + var combinedArgs = combine(args.clone(), arguments); + return __method.apply(this, combinedArgs); + } + } + + function delay() { + var __method = this, args = toArray(arguments), timeout = args.shift() * 1000; return window.setTimeout(function() { return __method.apply(__method, args); }, timeout); - }, - - defer: function() { - var args = [0.01].concat($A(arguments)); + } + + function defer() { + var args = combine([0.01], arguments); return this.delay.apply(this, args); - }, - - wrap: function(wrapper) { + } + + function wrap(wrapper) { var __method = this; return function() { - return wrapper.apply(this, [__method.bind(this)].concat($A(arguments))); + var combinedArgs = combine([__method.bind(this)], arguments); + return wrapper.apply(this, combinedArgs); } - }, - - methodize: function() { + } + + function methodize() { if (this._methodized) return this._methodized; var __method = this; return this._methodized = function() { - return __method.apply(null, [this].concat($A(arguments))); + var combinedArgs = combine([this], arguments); + return __method.apply(null, combinedArgs); }; } -}); \ No newline at end of file + + return { + argumentNames: argumentNames, + bind: bind, + bindAsEventListener: bindAsEventListener, + curry: curry, + delay: delay, + defer: defer, + wrap: wrap, + methodize: methodize + } +})()); +