Skip to content

Instantly share code, notes, and snippets.

@leonidkuznetsov18
Last active May 27, 2020 12:52
Show Gist options
  • Select an option

  • Save leonidkuznetsov18/ec92ad48cae9fae4704b0525016e6648 to your computer and use it in GitHub Desktop.

Select an option

Save leonidkuznetsov18/ec92ad48cae9fae4704b0525016e6648 to your computer and use it in GitHub Desktop.
bind es6
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