Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fun = function() {
- console.log(this);
- };
- console.log(this);
- fun();
- /*
- [Binding Pattern]
- 1. Global Reference
- 2. Free Function Invocation
- [Binding Target]
- : The global object(which is window in a browser)
- [What's it for?]
- : Not useful, but something had to be chosen as a binding for "this" in these cases
- */
- fun.call(obj);
- fun.apply(obj);
- /*
- [Binding Pattern]
- 3. .call or .apply Invocation
- [Binding Target]
- : The first argument of .call or .apply
- [What's it for?]
- : To manually specify a 'this' binding, like real arguments
- */
- new fun();
- /*
- [Binding Pattern]
- 4. Construction Mode
- [Binding Target]
- : A new object created for that invocation
- [What's it for?]
- : In order for constructors to operate on the instance they're creating
- */
- var obj = {
- method: fun
- };
- obj.method();
- /*
- [Binding Pattern]
- 5. Method Invocation
- [Binding Target]
- : Object on the left of the "call time" dot
- [What's it for?]
- : In order for methods to run in the context of an object they're in
- */
Add Comment
Please, Sign In to add comment