Add bind
This commit is contained in:
22
snippets/bind.md
Normal file
22
snippets/bind.md
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
### bind
|
||||||
|
|
||||||
|
Creates a function that invokes `fn` with a given context, optionally adding any additional supplied parameters to the beginning of the arguments.
|
||||||
|
|
||||||
|
Return a `function` that uses `Function.apply()` to apply the given `context` to `fn`.
|
||||||
|
Use `Array.concat()` to prepend any additional supplied parameters to the arguments.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const bind = (fn, context, ...args) =>
|
||||||
|
function() {
|
||||||
|
return fn.apply(context, args.concat(...arguments));
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
function greet(greeting, punctuation) {
|
||||||
|
return greeting + ' ' + this.user + punctuation;
|
||||||
|
}
|
||||||
|
const freddy = { 'user': 'fred' };
|
||||||
|
const freddyBound = bind(greet, freddy);
|
||||||
|
console.log(freddyBound('hi','!')); // 'hi fred!'
|
||||||
|
```
|
||||||
@ -4,6 +4,7 @@ ary:adapter,function
|
|||||||
atob:node,string,utility
|
atob:node,string,utility
|
||||||
average:math,array
|
average:math,array
|
||||||
averageBy:math,array,function
|
averageBy:math,array,function
|
||||||
|
bind:function,object
|
||||||
bottomVisible:browser
|
bottomVisible:browser
|
||||||
btoa:node,string,utility
|
btoa:node,string,utility
|
||||||
byteSize:string
|
byteSize:string
|
||||||
|
|||||||
Reference in New Issue
Block a user