Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
/** Used as the `TypeError` message for "Functions" methods. */
2
var FUNC_ERROR_TEXT = 'Expected a function';
3
4
/**
5
* Creates a function that invokes `func` with the `this` binding of the created
6
* function and an array of arguments much like [`Function#apply`](https://es5.github.io/#x15.3.4.3).
7
*
8
* **Note:** This method is based on the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator).
9
*
10
* @static
11
* @memberOf _
12
* @category Function
13
* @param {Function} func The function to spread arguments over.
14
* @returns {Function} Returns the new function.
15
* @example
16
*
17
* var say = _.spread(function(who, what) {
18
* return who + ' says ' + what;
19
* });
20
*
21
* say(['fred', 'hello']);
22
* // => 'fred says hello'
23
*
24
* // with a Promise
25
* var numbers = Promise.all([
26
* Promise.resolve(40),
27
* Promise.resolve(36)
28
* ]);
29
*
30
* numbers.then(_.spread(function(x, y) {
31
* return x + y;
32
* }));
33
* // => a Promise of 76
34
*/
35
function spread(func) {
36
if (typeof func != 'function') {
37
throw new TypeError(FUNC_ERROR_TEXT);
38
}
39
return function(array) {
40
return func.apply(this, array);
41
};
42
}
43
44
module.exports = spread;
45
46