mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
lodash: Add support for "lazy" bind. [jddalton]
Former-commit-id: 472c0436f7de4e636dd878900119008bf39592fa
This commit is contained in:
40
lodash.js
40
lodash.js
@@ -1432,30 +1432,58 @@
|
|||||||
/**
|
/**
|
||||||
* Creates a new function that, when called, invokes `func` with the `this`
|
* Creates a new function that, when called, invokes `func` with the `this`
|
||||||
* binding of `thisArg` and prepends additional arguments to those passed to
|
* binding of `thisArg` and prepends additional arguments to those passed to
|
||||||
* the bound function.
|
* the bound function. Lazy defined methods may be bound by passing the object
|
||||||
|
* they are bound to as `func` and the method name as `thisArg`.
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Functions
|
* @category Functions
|
||||||
* @param {Function} func The function to bind.
|
* @param {Function|Object} func The function to bind or the object the method belongs to.
|
||||||
* @param @param {Mixed} [thisArg] The `this` binding of `func`.
|
* @param @param {Mixed} [thisArg] The `this` binding of `func` or the method name.
|
||||||
* @param {Mixed} [arg1, arg2, ...] Arguments to prepend to those passed to the bound function.
|
* @param {Mixed} [arg1, arg2, ...] Arguments to prepend to those passed to the bound function.
|
||||||
* @returns {Function} Returns the new bound function.
|
* @returns {Function} Returns the new bound function.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
|
* // basic bind
|
||||||
* var func = function(greeting) { return greeting + ': ' + this.name; };
|
* var func = function(greeting) { return greeting + ': ' + this.name; };
|
||||||
* func = _.bind(func, { 'name': 'moe' }, 'hi');
|
* func = _.bind(func, { 'name': 'moe' }, 'hi');
|
||||||
* func();
|
* func();
|
||||||
* // => 'hi: moe'
|
* // => 'hi: moe'
|
||||||
|
*
|
||||||
|
* // lazy bind
|
||||||
|
* var object = {
|
||||||
|
* 'name': 'moe',
|
||||||
|
* 'greet': function(greeting) {
|
||||||
|
* return greeting + ': ' + this.name;
|
||||||
|
* }
|
||||||
|
* };
|
||||||
|
*
|
||||||
|
* var func = _.bind(object, 'greet', 'hi');
|
||||||
|
* func();
|
||||||
|
* // => 'hi: moe'
|
||||||
|
*
|
||||||
|
* object.greet = function(greeting) {
|
||||||
|
* return greeting + ' ' + this.name + '!';
|
||||||
|
* };
|
||||||
|
*
|
||||||
|
* func();
|
||||||
|
* // => 'hi moe!'
|
||||||
*/
|
*/
|
||||||
function bind(func, thisArg) {
|
function bind(func, thisArg) {
|
||||||
var args = slice.call(arguments, 2),
|
var args = slice.call(arguments, 2),
|
||||||
argsLength = args.length;
|
argsLength = args.length,
|
||||||
|
isFunc = toString.call(func) == funcClass;
|
||||||
|
|
||||||
|
// juggle arguments
|
||||||
|
if (!isFunc) {
|
||||||
|
var methodName = thisArg;
|
||||||
|
thisArg = func;
|
||||||
|
}
|
||||||
return function() {
|
return function() {
|
||||||
args.length = argsLength;
|
|
||||||
push.apply(args, arguments);
|
push.apply(args, arguments);
|
||||||
return func.apply(thisArg, args);
|
var result = (isFunc ? func : thisArg[methodName]).apply(thisArg, args);
|
||||||
|
args.length = argsLength;
|
||||||
|
return result;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
23
test/test.js
23
test/test.js
@@ -76,6 +76,29 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.bind');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
test('supports lazy bind', function() {
|
||||||
|
var object = {
|
||||||
|
'name': 'moe',
|
||||||
|
'greet': function(greeting) {
|
||||||
|
return greeting + ': ' + this.name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var func = _.bind(object, 'greet', 'hi');
|
||||||
|
equal(func(), 'hi: moe');
|
||||||
|
|
||||||
|
object.greet = function(greeting) {
|
||||||
|
return greeting + ' ' + this.name + '!';
|
||||||
|
};
|
||||||
|
equal(func(), 'hi moe!');
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.forEach');
|
QUnit.module('lodash.forEach');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user