Created
March 23, 2011 19:57
-
-
Save smoak/883831 to your computer and use it in GitHub Desktop.
This file contains 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
var myObject = { | |
property1: 'test', | |
myFunc: function(arg) { | |
// the 'this' object refers to the instance of the myObject variable | |
this.property1 = arg; | |
alert("Does this === myObject ? " + (this === myObject ? "yes" : "no") ); | |
var a = function() { | |
// inside a closure the 'this' object is now === window | |
alert("Does this === window ? " + (this === window ? "yes" : "no")); | |
// Therefore, to get at myObject's properties we must use it's name | |
alert(myObject.property1); // weee because this function is executed after myFunc("weee") is executed | |
}; | |
a(); | |
} | |
}; | |
alert(myObject.property1); // alerts test | |
// change the property1 property to something else | |
myObject.property1 = "zomg"; | |
alert(myObject.property1); // alerts zomg | |
// change the property1 to weee using myFunc | |
myObject.myFunc("weee"); | |
alert(myObject.property1); // alerts weee |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment