Last active
December 24, 2015 11:28
-
-
Save Cycymomo/6790775 to your computer and use it in GitHub Desktop.
toArray - convert array like javascript
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
(function () { | |
'use strict'; | |
var _slice = Array.prototype.slice; | |
try { | |
_slice.call(document.documentElement); // Can't be used with DOM elements in IE < 9 | |
} | |
catch (e) { // Fails in IE < 9 | |
Array.prototype.slice = function (begin, end) { | |
var i, arrl = this.length, a = []; | |
if (this.charAt) { // Although IE < 9 does not fail when applying Array.prototype.slice | |
// to strings, here we do have to duck-type to avoid failing | |
// with IE < 9's lack of support for string indexes | |
for (i = 0; i < arrl; i++) { | |
a.push(this.charAt(i)); | |
} | |
} | |
else { // This will work for genuine arrays, array-like objects, NamedNodeMap (attributes, entities, notations), NodeList (e.g., getElementsByTagName), HTMLCollection (e.g., childNodes), and will not fail on other DOM objects (as do DOM elements in IE < 9) | |
for (i = 0; i < this.length; i++) { // IE < 9 (at least IE < 9 mode in IE 10) does not work with node.attributes (NamedNodeMap) without a dynamically checked length here | |
a.push(this[i]); | |
} | |
} | |
return _slice.call(a, begin, end || a.length); // IE < 9 gives errors here if end is allowed as undefined (as opposed to just missing) so we default ourselves | |
}; | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment