Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ``` javascript
- function foo() {
- console.log(this.a);
- }
- var obj = {
- a: 2,
- foo: foo
- };
- //function reference/alias
- var bar = obj.foo;
- // ‘a’ is also property on global object
- var a = 'oops, global';
- bar(); // 'oops, global'
- /* even though bar appears to be a reference to obj.foo, in fact, it's really
- just another reference to foo itself. Moreover, the call-site is what matters,
- and the call-site is bar(), which is a plain, undecorated call,
- and thus the default binding applies. Here's another more subtle example
- that uses callbacks: */
- function foo() {
- console.log( this.a );
- }
- function doFoo(fn){
- // `fn` is just another reference to `foo`
- fn(); // <== call-site!
- }
- var obj = {
- a: 2,
- foo: foo
- };
- // `a` is also a property on the global object
- var a = 'oops, global';
- doFoo( obj.foo ); // 'oops, global'
- /* Parameter passing is just an implicit assignment, and since we're passing
- a function, it's an implicit reference assignment; as in the first example in this
- gist, obj.foo just refers to foo and it's executed in the global environment
- so THIS refers to the WINDOW */
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement