Last active
January 15, 2018 06:50
-
-
Save YozhEzhi/2f8feaa60b40bba672c4de973f524dcc to your computer and use it in GitHub Desktop.
ES6 custom bind
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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