Last active
February 28, 2019 23:02
-
-
Save michaellzc/ce85b2848e208e6483dfd133f1ee17de to your computer and use it in GitHub Desktop.
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(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