HTML and CSS Reference
In-Depth Information
Listing 5.7 Using arguments
function assert(message, expr) {
if (arguments.length < 2) {
throw new Error("Provide message and value to test");
if (!arguments[1]) {
throw new Error(arguments[0]);
return true;
assert.count = 0;
This is not a particularly useful way to use arguments , but shows how it works.
In general, the arguments object should only be used when formal parameters
cannot solve the problem at hand, because using it comes with a performance price.
In fact, merely referencing the object will induce some overhead, indicating that
browsers optimize functions that don't use it.
The arguments object is array-like only through its length property and
numeric index properties; it does not provide array methods. Still, we can use
array methods on it by utilizing Array.prototype.* and their call or apply
methods. Listing 5.8 shows an example in which we create an array consisting of all
but the first argument to a function.
Listing 5.8 Using array methods with arguments
function addToArray() {
var targetArr = arguments[0];
var add =, 1);
return targetArr.concat(add);
As with arrays, the numerical indexes on the arguments object are really only
properties with numbers for identifiers. Object identifiers are always converted to
strings in JavaScript, which explains the code in Listing 5.9.
Search WWH ::

Custom Search