Last active
October 28, 2015 13:45
-
-
Save bahmutov/3ee687adb48b9f150f4d to your computer and use it in GitHub Desktop.
Mutate the parent closure named arguments via arguments from outside
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
// NOTE does not work in strict mode | |
function parent(a, b) { | |
console.log('in parent, a', a, 'b', b); | |
var parentArguments = arguments; | |
function child() { | |
console.log('in child, a', a, 'b', b); | |
console.log('is parentArguments === c.args?', parentArguments === c.args); | |
console.log('parentArguments', parentArguments); | |
return a + b; | |
} | |
child.args = parentArguments; | |
return child; | |
} | |
var c = parent(0, 1); | |
// can we change a and b inside the "c"? | |
c.args['0'] = 'foo'; | |
c.args['1'] = 'bar'; | |
console.log(c()); | |
// "foobar" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One can even pass list of undefined to the
parent
function - as long as the arguments number is >= expected it is working, one can mutate the named arguments from the outside