Skip to content

Instantly share code, notes, and snippets.

@smoak
Created March 23, 2011 19:57
Show Gist options
  • Save smoak/883831 to your computer and use it in GitHub Desktop.
Save smoak/883831 to your computer and use it in GitHub Desktop.
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