Guest User

Untitled

a guest
Jan 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. // global code
  2. this.foo = 1; // creating a global property
  3. foo // accessing the global property via a direct reference
  4.  
  5. // global code
  6. var foo = 1; // creating a global variable
  7. this.foo // accessing the global variable via a property reference
  8.  
  9. foo // => undefined
  10. bar // throws ReferenceError
  11.  
  12. var foo = 1;
  13. this.bar = 1;
  14.  
  15. // the names "foo" and "bar" are bound to the global environment
  16. var foo = 1;
  17. this.bar = 1;
  18.  
  19. // the binding "bar" can be removed from the global environment subsequently
  20. delete this.bar;
  21.  
  22. // the binding "foo" cannot be removed subsequently
  23.  
  24. Object.defineProperty( this, 'bar', { value: 1 }); // non-configurable by default
  25.  
  26. // "this" refers to the global object. But global object is also acting as the
  27. // variable object! Because of that, the following code works:
  28.  
  29. var foo = 1;
  30. alert(this.foo); // 1
  31.  
  32. (function() {
  33.  
  34. // "this" still refers to the global object! But the *variable* object has
  35. // changed because we're now in the execution context of a function, thus
  36. // the behavior changes:
  37.  
  38. var bar = 2;
  39. alert(this.foo); // 1
  40. alert(this.bar); // undefined
  41.  
  42. })();
Add Comment
Please, Sign In to add comment