daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest May 25th, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fun = function() {
  2.   console.log(this);
  3. };
  4.  
  5. console.log(this);
  6. fun();
  7. /*
  8.   [Binding Pattern]
  9.     1. Global Reference
  10.     2. Free Function Invocation
  11.   [Binding Target] 
  12.     : The global object(which is window in a browser)
  13.   [What's it for?] 
  14.     : Not useful, but something had to be chosen as a binding for "this" in these cases
  15. */
  16.  
  17. fun.call(obj);
  18. fun.apply(obj);
  19. /*
  20.   [Binding Pattern]
  21.     3. .call or .apply Invocation
  22.   [Binding Target] 
  23.     : The first argument of .call or .apply
  24.   [What's it for?] 
  25.     : To manually specify a 'this' binding, like real arguments
  26. */
  27.  
  28. new fun();
  29. /*
  30.   [Binding Pattern]
  31.     4. Construction Mode
  32.   [Binding Target] 
  33.     : A new object created for that invocation
  34.   [What's it for?] 
  35.     : In order for constructors to operate on the instance they're creating
  36. */
  37.  
  38. var obj = {
  39.   method: fun
  40. };
  41. obj.method();
  42. /*
  43.   [Binding Pattern]
  44.     5. Method Invocation
  45.   [Binding Target] 
  46.     : Object on the left of the "call time" dot
  47.   [What's it for?] 
  48.     : In order for methods to run in the context of an object they're in
  49. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top