diff --git a/README.md b/README.md index dae4b557a..39697ed58 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,7 @@ average(1, 2, 3);
View contents +* [`bind`](#bind) * [`chainAsync`](#chainasync) * [`compose`](#compose) * [`composeRight`](#composeright) @@ -3153,6 +3154,37 @@ tomorrow(); // 2017-12-27 (if current date is 2017-12-26) --- ## 🎛️ Function +### 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)); + }; +``` + +
+Examples + +```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!' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### chainAsync Chains asynchronous functions. diff --git a/docs/index.html b/docs/index.html index db0ed852a..708ed4bb5 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -691,7 +691,17 @@ document.bodypadStart(2, '0')}`;
 };
 
tomorrow(); // 2017-12-27 (if current date is 2017-12-26)
-

Function

chainAsync

Chains asynchronous functions.

Loop through an array of functions containing asynchronous events, calling next when each asynchronous event has completed.

const chainAsync = fns => {
+

Function

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.

const bind = (fn, context, ...args) =>
+  function() {
+    return fn.apply(context, args.concat(...arguments));
+  };
+
function greet(greeting, punctuation) {
+  return greeting + ' ' + this.user + punctuation;
+}
+const freddy = { user: 'fred' };
+const freddyBound = bind(greet, freddy);
+console.log(freddyBound('hi', '!')); // 'hi fred!'
+

chainAsync

Chains asynchronous functions.

Loop through an array of functions containing asynchronous events, calling next when each asynchronous event has completed.

const chainAsync = fns => {
   let curr = 0;
   const next = () => fns[curr++](next);
   next();
diff --git a/snippets/bind.md b/snippets/bind.md
index 73e1ab253..c0a9402e2 100644
--- a/snippets/bind.md
+++ b/snippets/bind.md
@@ -16,7 +16,7 @@ const bind = (fn, context, ...args) =>
 function greet(greeting, punctuation) {
   return greeting + ' ' + this.user + punctuation;
 }
-const freddy = { 'user': 'fred' };
+const freddy = { user: 'fred' };
 const freddyBound = bind(greet, freddy);
-console.log(freddyBound('hi','!')); // 'hi fred!'
+console.log(freddyBound('hi', '!')); // 'hi fred!'
 ```