Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // global code
- this.foo = 1; // creating a global property
- foo // accessing the global property via a direct reference
- // global code
- var foo = 1; // creating a global variable
- this.foo // accessing the global variable via a property reference
- foo // => undefined
- bar // throws ReferenceError
- var foo = 1;
- this.bar = 1;
- // the names "foo" and "bar" are bound to the global environment
- var foo = 1;
- this.bar = 1;
- // the binding "bar" can be removed from the global environment subsequently
- delete this.bar;
- // the binding "foo" cannot be removed subsequently
- Object.defineProperty( this, 'bar', { value: 1 }); // non-configurable by default
- // "this" refers to the global object. But global object is also acting as the
- // variable object! Because of that, the following code works:
- var foo = 1;
- alert(this.foo); // 1
- (function() {
- // "this" still refers to the global object! But the *variable* object has
- // changed because we're now in the execution context of a function, thus
- // the behavior changes:
- var bar = 2;
- alert(this.foo); // 1
- alert(this.bar); // undefined
- })();
Add Comment
Please, Sign In to add comment