1 /**
  2  * @fileOverview Extensions/helper methods for Functions
  3  * @author <a href="http://www.bobs-bits.com">Stephen Reay</a>
  4  */
  5 
  6 if (!Function.prototype.bind) {
  7 	/**
  8 	 * Bind a function to an object and return it as a callback
  9 	 * @param {Object} context the object to bind the function to
 10 	 * @param {arguments} [1..n] arguments to be passed to the function in the callback
 11 	 * @returns {Function} a callback to the function bound to the object
 12 	 * @requires Object#toArray
 13 	 * @example
 14 	 * var myInstance = new MyClass();
 15 	 * var callback = myInstance.myMethod.bind(myInstance); //context-safe callback
 16 	 */
 17 	Function.prototype.bind = function bind(context) {
 18 		var callback = this;
 19 		var args = Object.toArray(arguments);
 20 		var object = args.shift();
 21 		return function () {
 22 			return callback.apply(object, args.concat(Object.toArray(arguments)));
 23 		};
 24 	};
 25 }
 26 
 27 if (!Function.prototype.bindAsEventListener) {
 28 	/**
 29 	 * Bind a function to an object and return it as a callback that recieves the active Event as its first argument
 30 	 * @param {Object} context the object to bind the function to
 31 	 * @param {arguments} [1..n] arguments to be passed to the function in the callback
 32 	 * @returns {Function} a callback to the function bound to the object
 33 	 * @see Function#bind
 34 	 * @example
 35 	 * var myInstance = new MyClass();
 36 	 * var callback = myInstance.myMethod.bind(myInstance); //context-safe callback
 37 	 */
 38 	Function.prototype.bindAsEventListener = function bindAsEventListener(context) {
 39 		var callback = this;
 40 		var args = Object.toArray(arguments);
 41 		var object = args.shift();
 42 		return function (event) {
 43 			return callback.apply(object, [event || window.event].concat(args));
 44 		};
 45 	};
 46 }
 47 
 48 if (!Function.prototype.curry) {
 49 	/**
 50 	 * Create a callback with some arguments pre-specified
 51 	 * @param {Mixed} 1-n the arguments to pre-apply to the callback
 52 	 * @returns {Function} the pre-applied callback
 53 	 * @example
 54 	 * function myFunc(pre, text) {
 55 	 * 		return pre + text;
 56 	 * }
 57 	 * ['car', 'house', 'dog'].map(myFunc.curry('Your')); // Returns ['Your car', 'Your house', 'Your dog']
 58 	 */
 59 	Function.prototype.curry = function curry() {
 60 		var callback = this;
 61 		var args = Object.toArray(arguments);
 62 		return function () {
 63 			return callback.apply(null, args.concat(Object.toArray(arguments)));
 64 		};
 65 	};
 66 }
 67 
 68 if (!Function.prototype.inherits) {
 69 	/**
 70 	 * Makes a Class constructor Function inherit from another Class
 71 	 * @param {Class} parentClass the Class to inherit from
 72 	 * @author <a href="http://www.coolpage.com/developer/javascript/Correct%20OOP%20for%20Javascript.html">Shelby H. Moore III</a>
 73 	 * @see Object#inherits
 74 	 * @example
 75 	 * function ClassA(arg) {
 76 	 *     this.prop = arg || false;
 77 	 * }
 78 	 * 
 79 	 * ClassB.inherits(ClassA);
 80 	 * function ClassB(arg) {
 81 	 *     this.inherits(ClassA);
 82 	 *     this.prop2 = arg || 'default';
 83 	 * }
 84 	 */
 85 	Function.prototype.inherits = function inherits(ParentClass) {
 86 		if (this === ParentClass) {
 87 	 		throw new ReferenceError("A Class can't inherit from itself");
 88 	 	}
 89 		
 90 		this.prototype = new ParentClass();
 91 		this.prototype.constructor = this;
 92 	};
 93 }