From 089b81ff2e02bec187c53e0ddc8678888bb3774c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 26 Jan 2018 14:14:53 +0200 Subject: [PATCH] Add bindAll --- snippets/bindAll.md | 26 ++++++++++++++++++++++++++ tag_database | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 snippets/bindAll.md diff --git a/snippets/bindAll.md b/snippets/bindAll.md new file mode 100644 index 000000000..0fcd01a90 --- /dev/null +++ b/snippets/bindAll.md @@ -0,0 +1,26 @@ +### bindAll + +Explain briefly what the snippet does. + +Use `Array.forEach()` to return a `function` that uses `Function.apply()` to apply the given context (`obj`) to `fn` for each function specified. + +```js +const bindAll = (obj, ...fns) => + fns.forEach( + fn => + (obj[fn] = function() { + return fn.apply(obj); + }) + ); +``` + +```js +var view = { + 'label': 'docs', + 'click': function() { + console.log('clicked ' + this.label); + } +}; +bindAll(view, 'click'); +jQuery(element).on('click', view.click); // Logs 'clicked docs' when clicked. +``` diff --git a/tag_database b/tag_database index 4e20c4596..9a84845df 100644 --- a/tag_database +++ b/tag_database @@ -5,6 +5,7 @@ atob:node,string,utility average:math,array averageBy:math,array,function bind:function,object +bindAll:object,function bindKey:function,object bottomVisible:browser btoa:node,string,utility @@ -214,7 +215,7 @@ sortedLastIndexBy:array,math,function splitLines:string spreadOver:adapter standardDeviation:math,array -stripHTMLtags:uncategorized +stripHTMLTags:string,utility,regexp sum:math,array sumBy:math,array,function sumPower:math