Created
March 12, 2014 16:11
-
-
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.
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
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