JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

Arguments.length

Arguments.length


the number of arguments passed to a function

Synopsis

arguments.length

Description

The length property of the Arguments object specifies the number of arguments passed to the current function. This property is defined only within a function body.

Note that this property specifies the number of arguments actually passed, not the number expected. See Function.length for the number of declared arguments. Note also that this property does not have any of the special behavior of the Array.length property.

Example

// Use an Arguments object to check that correct # of args were passed

function check(args) { var actual = args.length; // The actual number of arguments var expected = args.callee.length; // The expected number of arguments if (actual != expected) { // Throw exception if they don't match

throw new Error("Wrong number of arguments: expected: " + expected + "; actually passed " + actual);

} } // A function that demonstrates how to use the function above function f(x, y, z) {

check(arguments); // Check for correct number of arguments return x + y + z; // Now do the rest of the function normally }

See Also

Array.length , Function.length

Array

built-in support for arrays Object → Array

Core JavaScript Reference | 721

Array

Constructor

new Array() new Array(size) new Array(element0, element1, ..., elementn)

Arguments

size

The desired number of elements in the array. The returned array has its length field set to size.

element0, ... elementn

An argument list of two or more arbitrary values. When the Array() constructor is invoked with these arguments, the newly created array is initialized with the specified argument values as its elements and its length field set to the number of arguments.

Returns

The newly created and initialized array. When Array() is invoked with no arguments, the returned array is empty and has a length field of 0. When invoked with a single numeric argument, the constructor returns an array with the specified number of undefined elements. When invoked with any other arguments, the constructor initializes the array with the values specified by the arguments. When the Array()constructor is called as a function, without the new operator, it behaves exactly as it does when called with the new operator.

Throws

RangeError When a single integer size argument is passed to the Array() constructor, a RangeError exception is thrown if size is negative or is larger than 232−1.

Literal Syntax

ECMAScript v3 specifies an array literal syntax. You may also create and initialize an array by placing a comma-separated list of expressions within square brackets. The values of these expressions become the elements of the array. For example:

var a = [1, true, 'abc']; var b = [a[0], a[0]*2, f(x)];

Properties

length

A read/write integer specifying the number of elements in the array or, when the array does not have contiguous elements, a number one larger than the index of the last element in the array. Changing the value of this property truncates or extends the array.

Methods

The methods every(), filter(), forEach(), indexOf(), lastIndexOf(), map(), reduce(), reduce Right(), and some() are new in ECMAScript 5 but were implemented by browsers other than IE before ES5 was standardized.

Array

concat()

Concatenates elements to an array.

every()

Test whether a predicate is true for every array element.

filter()

Return array elements that satisfy a predicate function.

forEach()

Invoke a function for each element of the array.

indexOf()

Search an array for a matching element.

join()

Converts all array elements to strings and concatenates them.

lastIndexOf()

Search backward through an array.

map()

Compute new array elements from the elements of this array.

pop()

Removes an item from the end of an array.

push()

Pushes an item to the end of an array.

reduce()

Compute a value from the elements of this array.

reduceRight()

Reduce this array from right-to-left.

reverse()

Reverses, in place, the order of the elements of an array.

shift()

Shifts an element off the beginning of an array.

slice()

Returns a subarray slice of an array.

some()

Test whether a predicate is true for at least one element of this array.

sort()

Sorts, in place, the elements of an array.

splice()

Inserts, deletes, or replaces array elements.

toLocaleString()

Converts an array to a localized string.

Core JavaScript Reference | 723

Array.concat()

toString()

Converts an array to a string.

unshift()

Inserts elements at the beginning of an array.

Description

Arrays are a basic feature of JavaScript and are documented in detail in Chapter 7 .

See Also

Chapter 7

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/28174.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1