Skip to content

Instantly share code, notes, and snippets.

@jvnlwn
Created March 12, 2014 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvnlwn/9510168 to your computer and use it in GitHub Desktop.
Save jvnlwn/9510168 to your computer and use it in GitHub Desktop.
Fooling around with closures. Defined a function that accepts an argument and returns a function that manipulates said argument. I believe my gist is just an example of object referencing.
function what(obj) {
return function(str) {
if (str) {
console.log('name WAS: ', obj.name)
obj.name = str;
}
console.log('name is: ', obj.name)
}
}
function scope() {
var obj = {name: 'yo baby'}
return {
myName: what(obj),
yourName: what(obj)
}
}
var obj = scope();
obj.myName('hot stuff')
// -> name WAS: yo baby
// -> name is: hot stuff
obj.yourName()
// -> name is: hot stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment