Created
August 24, 2014 04:02
-
-
Save bion/589337697fcb02415e99 to your computer and use it in GitHub Desktop.
Javascript context bindng
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| in functions evaluated 'anonymously', that is without a reference to an object, | |
| the 'this' keyword will reference the global object. in a web page that will end | |
| up being 'Window', in node it will be the node process itself. | |
| 'this.number' will be undefined here because the global object has no property called 'number' | |
| */ | |
| var one = function() { | |
| console.log(this.number + ": 'this' referenced inside bare function binds to the global object:", this); | |
| }; | |
| one(); | |
| /* | |
| when a function is evaluated as a method of an object, 'this' will reference that object, | |
| 'this.number' will therefore be the value of the 'number' property we defined on that object | |
| */ | |
| var two = { | |
| print: function() { | |
| console.log(this.number + ": 'this' referenced inside function called as method on 'two':", this); | |
| }, | |
| number: 2 | |
| }; | |
| two.print(); | |
| /* | |
| anymousFunc here is defined in the local scope of the function three.print. so | |
| when it gets evaluated, 'this' inside of it references the global object because anonymousFunc | |
| itself is not a property of 'three' | |
| */ | |
| var three = { | |
| print: function() { | |
| function anonymousFunc() { | |
| console.log(this.number + ": 'this' referenced inside function invoked anonymously:", this); | |
| }; | |
| anonymousFunc(); | |
| }, | |
| number: 3 | |
| }; | |
| three.print(); | |
| /* | |
| when you assign a variable to a function that is a property of an object, and then evaluate | |
| it later, the function will no longer have a reference to the object it was defined as a property of | |
| */ | |
| var four = { | |
| print: function() { | |
| console.log(this.number + ": 'this' referenced inside function called anonymously, even though defined as a property on an object:", this); | |
| }, | |
| number: 4 | |
| }; | |
| var anonymousFuncAgain = four.print; | |
| anonymousFuncAgain(); // this is still four.print, but it doesn't know how to reference four anymore | |
| /* | |
| there are several ways to preserve the 'this' binding when this is done, 'call' and 'apply' | |
| are both methods defined on all functions that invoke the function with a 'this' binding passed in | |
| */ | |
| var five = { | |
| print: function() { | |
| console.log(this.number + ": 'this' here is the 'five' object because the call() function bound it for us:", this); | |
| }, | |
| number: 5 | |
| }; | |
| var anonymousFive = five.print; | |
| anonymousFive.call(five); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment