Skip to content

Instantly share code, notes, and snippets.

@bahmutov
Last active October 28, 2015 13:45
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 bahmutov/3ee687adb48b9f150f4d to your computer and use it in GitHub Desktop.
Save bahmutov/3ee687adb48b9f150f4d to your computer and use it in GitHub Desktop.
Mutate the parent closure named arguments via arguments from outside
// 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"
@bahmutov
Copy link
Author

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

var c = parent.apply(null, new Array(parent.length));

@ggoodman
Copy link

Smells like a bug to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment