Last active
May 27, 2020 12:52
-
-
Save leonidkuznetsov18/ec92ad48cae9fae4704b0525016e6648 to your computer and use it in GitHub Desktop.
bind es6
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
| function foo(a, b) { | |
| console.log("___A___", a); | |
| console.log("___B___", b); | |
| return a + b + this.c; | |
| } | |
| function bind(fn, ctx, ...args) { | |
| if(typeof(fn) !== 'function') { | |
| throw new TypeError("error"); | |
| } | |
| return function(args2) { | |
| return fn.apply(ctx, [...args, ...args2]) // order important | |
| } | |
| } | |
| const f1 = bind(foo, { c: 42 }); | |
| const f2 = bind(foo, { c: 42 }, 10); | |
| console.assert(f1(1, 2) === 45, "f1(1, 2) === 45"); | |
| console.assert(f2(1, 2) === 53, "f2(1, 2) === 53"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment