Created
May 19, 2012 22:52
-
-
Save minitech/2732683 to your computer and use it in GitHub Desktop.
Why you should use my new way to convert to an array
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
// The data | |
var items = document.getElementsByTagName('p'); | |
// The old way | |
var arr = Array.prototype.slice.call(items); | |
// Pros: | |
// - Doesn't need a helper method that loops through stuff | |
// | |
// Cons: | |
// - Verbose | |
// - Doesn't work in IE 8 or lower | |
// The new way | |
var arr = Array.apply([], items); | |
// Pros: | |
// - Doesn't need a helper method that loops through stuff | |
// - Reasonably concise | |
// - Works in IE 7 and 8 at least | |
// | |
// Cons: | |
// - None that I can think of! Take that, old way! (jsPerf test pending.) | |
// Awesome new way brought to you by Ryan O'Hara (minitech) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Argh. Right. And yes,
null
is better,[]
is left over from other experiments.@js-coder: Still doesn't work in IE, though - that's what I'm trying to fix in a non-frustrating way.