Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
oatkiller edited this page Sep 13, 2010 · 5 revisions

description

use array to get an array from the arguments variable that functions create.

usage

(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');

})();

details

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.

more

this uses slice as a generic:

Clone this wiki locally