mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 15:57:48 +00:00
Bump to v3.0.0.
This commit is contained in:
58
function/bind.js
Normal file
58
function/bind.js
Normal file
@@ -0,0 +1,58 @@
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
import createWrapper from '../internal/createWrapper';
|
||||
import replaceHolders from '../internal/replaceHolders';
|
||||
|
||||
/** Used to compose bitmasks for wrapper metadata. */
|
||||
var BIND_FLAG = 1,
|
||||
PARTIAL_FLAG = 32;
|
||||
|
||||
/**
|
||||
* Creates a function that invokes `func` with the `this` binding of `thisArg`
|
||||
* and prepends any additional `_.bind` arguments to those provided to the
|
||||
* bound function.
|
||||
*
|
||||
* The `_.bind.placeholder` value, which defaults to `_` in monolithic builds,
|
||||
* may be used as a placeholder for partially applied arguments.
|
||||
*
|
||||
* **Note:** Unlike native `Function#bind` this method does not set the `length`
|
||||
* property of bound functions.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Function
|
||||
* @param {Function} func The function to bind.
|
||||
* @param {*} thisArg The `this` binding of `func`.
|
||||
* @param {...*} [args] The arguments to be partially applied.
|
||||
* @returns {Function} Returns the new bound function.
|
||||
* @example
|
||||
*
|
||||
* var greet = function(greeting, punctuation) {
|
||||
* return greeting + ' ' + this.user + punctuation;
|
||||
* };
|
||||
*
|
||||
* var object = { 'user': 'fred' };
|
||||
*
|
||||
* var bound = _.bind(greet, object, 'hi');
|
||||
* bound('!');
|
||||
* // => 'hi fred!'
|
||||
*
|
||||
* // using placeholders
|
||||
* var bound = _.bind(greet, object, _, '!');
|
||||
* bound('hi');
|
||||
* // => 'hi fred!'
|
||||
*/
|
||||
function bind(func, thisArg) {
|
||||
var bitmask = BIND_FLAG;
|
||||
if (arguments.length > 2) {
|
||||
var partials = baseSlice(arguments, 2),
|
||||
holders = replaceHolders(partials, bind.placeholder);
|
||||
|
||||
bitmask |= PARTIAL_FLAG;
|
||||
}
|
||||
return createWrapper(func, bitmask, thisArg, partials, holders);
|
||||
}
|
||||
|
||||
// Assign default placeholders.
|
||||
bind.placeholder = {};
|
||||
|
||||
export default bind;
|
||||
Reference in New Issue
Block a user