Last active
March 8, 2016 11:06
-
-
Save WebReflection/4327762cb87a8c634a29 to your computer and use it in GitHub Desktop.
How to slice arguments without leaking them
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
// Andrea Giammarchi, WTFPL | |
function slice() {'use strict'; | |
for (var | |
o = +this, // offset | |
i = o, // start index | |
l = arguments.length, // length | |
n = l - o, // new length | |
a = Array(n < 0 ? 0 : n); // new Array | |
i < l; i++ | |
) a[i - o] = arguments[i]; | |
return a; | |
} | |
/** | |
* @example | |
(function () { | |
slice.apply(0, arguments); // [1,2,3] | |
slice.apply(1, arguments); // [2,3] | |
slice.apply(6, arguments); // [] | |
}(1,2,3)); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is why leaking arguments is bad
And this is why using
apply
is safe