Skip to content

Instantly share code, notes, and snippets.

@YozhEzhi
Last active January 15, 2018 06:50
Show Gist options
  • Select an option

  • Save YozhEzhi/2f8feaa60b40bba672c4de973f524dcc to your computer and use it in GitHub Desktop.

Select an option

Save YozhEzhi/2f8feaa60b40bba672c4de973f524dcc to your computer and use it in GitHub Desktop.
ES6 custom bind
const bind = (target, context, ...bindedArgs) =>
(...args) => target.call(context, ...bindedArgs, ...args);
// Example 1.
const sumFn = function(...args) {
return [...args].reduce((prev, next) => prev + next, this.value);
};
const bindedSum = bind(sumFn, {value: 10}, 20, 30);
console.log(bindedSum(40, 50, 60)); // 210
// Example 2.
const list = (...args) => [...args];
const leadingThirtysevenList = bind(list, undefined, 37);
console.log(leadingThirtysevenList()); // [37]
console.log(leadingThirtysevenList(1, 2, 3)); // [37, 1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment