This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
array
oatkiller edited this page Sep 13, 2010
·
5 revisions
use array to get an array from the arguments variable that functions create.
(function () {
var my_fn = function () {
// all functions get a var called arguments made for them.
// arguments looks like an array, but isnt.
// it has properties number 0 through (its length - 1) and a length property.
// but it doesnt have the methods arrays have, like push or concat
var args = arguments;
// use the array function to get an array from that array-like object
var array_of_args = o.array(arguments);
// now that you have an actual array.
// you can use the push method on it (all arrays get a push method)
array_of_args.push('d');
arguments.push('e') // throws an error!
};
my_fn('a','b','c');
})();
use array to get an array from an array-like object. that is, pass array an object that has properties from 0 through its length – 1, and a length property. array will return an array with the same properties. this function uses Array.prototype.slice (which is a generic) to efficiently create this.
this uses slice as a generic:
- more about generics: mdcs page on array, which has a section listing generics and a breif description
- more about slice: mdcs page on slice