Skip to content

Instantly share code, notes, and snippets.

@michaellzc
Last active February 28, 2019 23:02
Show Gist options
  • Select an option

  • Save michaellzc/ce85b2848e208e6483dfd133f1ee17de to your computer and use it in GitHub Desktop.

Select an option

Save michaellzc/ce85b2848e208e6483dfd133f1ee17de to your computer and use it in GitHub Desktop.
function foo(b) {
console.log(this.a, b);
return this.a + b;
}
var context = {
a: 2,
};
var fooFake = foo;
var fooClone = foo.bind(context); // The bind make sure fooClone works exactly the same as foo.
var fooAlsoWork = function() {
return foo.apply(context, arguments);
};
var fFake = foo(3); // undefined 3
console.log(fFake); // NaN
var f1 = fooClone(3); // 2 3
console.log(f1); // 5
var f2 = fooAlsoWork(3); // 2 3
console.log(f2); // 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment