Last active
November 2, 2017 08:19
-
-
Save web2ls/ff1ea22133845a2650954484ef6f94e1 to your computer and use it in GitHub Desktop.
Works with function arguments in JS
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
| /*Only use: | |
| arguments.length | |
| arguments[i] where i is always a valid integer index into the arguments, and can not be out of bound | |
| Never use arguments directly without .length or [i] | |
| STRICTLY fn.apply(y, arguments) is ok, nothing else is, e.g. .slice. Function#apply is special. | |
| Workaround for proxying is to create array in-line:*/ | |
| function doesntLeakArguments() { | |
| //.length is just an integer, this doesn't leak | |
| //the arguments object itself | |
| var args = new Array(arguments.length); | |
| for(var i = 0; i < args.length; ++i) { | |
| //i is always valid index in the arguments object | |
| args[i] = arguments[i]; | |
| } | |
| return args; | |
| } | |
| function anotherNotLeakingExample() { | |
| var i = arguments.length; | |
| var args = []; | |
| while (i--) args[i] = arguments[i]; | |
| return args | |
| } | |
| var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment