UNPKG

itsa-jsext

Version:

Extensions to native javascript-objects, all within the itsa_ namespace

53 lines (48 loc) 1.62 kB
/** * * Pollyfils for often used functionality for Functions * * <i>Copyright (c) 2014 ITSA - https://github.com/itsa</i> * New BSD License - http://choosealicense.com/licenses/bsd-3-clause/ * * @module js-ext * @submodule lib/function.js * @class Function * */ "use strict"; (function(FunctionPrototype) { /** * Sets the context of which the function will be execute. in the * supplied object's context, optionally adding any additional * supplied parameters to the end of the arguments the function * is executed with. * * @method itsa_rbind * @param [context] {Object} the execution context. * The value is ignored if the bound function is constructed using the new operator. * @param [args*] {any} args* 0..n arguments to append to the end of * arguments collection supplied to the function. * @return {function} the wrapped function. */ FunctionPrototype.itsa_rbind = function (context /*, args* */ ) { var thisFunction = this, arrayArgs, slice = Array.prototype.slice; context || (context = this); if (arguments.length > 1) { // removing `context` (first item) by slicing it out: arrayArgs = slice.call(arguments, 1); } return (arrayArgs ? function () { // over here, `arguments` will be the "new" arguments when the final function is called! return thisFunction.apply(context, slice.call(arguments, 0).concat(arrayArgs)); } : function () { // over here, `arguments` will be the "new" arguments when the final function is called! return thisFunction.apply(context, arguments); } ); }; }(Function.prototype));