Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active March 8, 2016 11:06
Show Gist options
  • Save WebReflection/4327762cb87a8c634a29 to your computer and use it in GitHub Desktop.
Save WebReflection/4327762cb87a8c634a29 to your computer and use it in GitHub Desktop.
How to slice arguments without leaking them
// 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));
*/
@WebReflection
Copy link
Author

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