Skip to content

Instantly share code, notes, and snippets.

@grahamlyons
Created November 22, 2011 12:14
Show Gist options
  • Save grahamlyons/1385531 to your computer and use it in GitHub Desktop.
Save grahamlyons/1385531 to your computer and use it in GitHub Desktop.
Fred's Javascript scoping
function Fred(state){
var innerState = state;
return {
getState: function() {
return innerState;
},
setState: function(newState){
innerState = newState;
}
};
}
var fred = new Fred("OK");
console.log(fred.getState());
fred.setState("Confused");
console.log(fred.getState());
//Both undefined in this scope
console.log(this.innerState);
console.log(fred.innerState);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment