Skip to content

Instantly share code, notes, and snippets.

@luavixen
Created September 14, 2020 05:35
Show Gist options
  • Save luavixen/05081b4b844c660b562d6c3c75fb7879 to your computer and use it in GitHub Desktop.
Save luavixen/05081b4b844c660b562d6c3c75fb7879 to your computer and use it in GitHub Desktop.
Object.prototype.toString is a thing, so how about toArray?
function toArray() {
var length = this.length;
if (
typeof length === "number"
&& length === length
&& length > 0
) {
var array = new Array(length);
for (var i = 0; i < length; i++) {
array[i] = this[i];
}
return array;
}
return [];
}
Object.prototype.toArray = toArray;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment