Skip to content

Instantly share code, notes, and snippets.

@leobalter
Forked from rwaldron/array.extensions.md
Created July 10, 2011 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leobalter/1074143 to your computer and use it in GitHub Desktop.
Save leobalter/1074143 to your computer and use it in GitHub Desktop.
Array goodies from twitter rap with David Herman, see wrap up here: http://twitter.com/#!/littlecalculist/status/89855977838485504
// http://twitter.com/#!/littlecalculist/status/89848378682392576
// http://twitter.com/#!/littlecalculist/status/89855977838485504
// Variable Arity Array.of()
Array.of = function() {
return [].slice.call( arguments );
};
console.log(
Array.of( "things", "that", "aren't", "currently", "an", "array" )
);
// ["things", "that", "aren't", "currently", "an", "array"]
console.log(
// This is a given, but shows a consistency in expected behaviour
Array.of.apply( null, [ "foo", "bar", "baz" ] )
);
// [ "foo", "bar", "baz" ]
// Unary Array.from()
Array.from = function( arrayish ) {
return [].slice.call( arrayish );
};
Array.from( NodeList );
Array.from( arguments );
Array.from( DOMTokenList ); // eg. classList
// see console
console.log(
Array.from( document.querySelectorAll("*") )
);
Array.from( document.querySelectorAll("*") ).forEach(function( node ) {
console.log( node );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment