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

Array.pop()

Array.pop()


remove and return the last element of an array

Synopsis

array.pop()

Returns

The last element of array.

Description

pop() deletes the last element of array, decrements the array length, and returns the value of the element that it deleted. If the array is already empty, pop() does not change the array and returns the undefined value.

Example

pop(), and its companion method push(), provide the functionality of a first-in, last-out stack. For example:

var stack = []; // stack: [] stack.push(1, 2); // stack: [1,2] Returns 2 stack.pop(); // stack: [1] Returns 2 stack.push([4,5]); // stack: [1,[4,5]] Returns 2 stack.pop() // stack: [1] Returns [4,5] stack.pop(); // stack: [] Returns 1

See Also

Array.push()

Array.push()

append elements to an array

Synopsis

array.push(value, ...)

Arguments

value, ... One or more values to be appended to the end of array.

Core JavaScript Reference | 731

Array.reduce()

Returns

The new length of the array, after the specified values are appended to it.

Description

push() appends its arguments, in order, to the end of array. It modifies array directly, rather than creating a new array. push(), and its companion method pop(), use arrays to provide the functionality of a first in, last out stack. See Array.pop() for an example.

See Also

Array.pop()

Array.reduce() ECMAScript 5

compute a value from the elements of an array

Synopsis

array.reduce(f) array.reduce(f, initial)

Arguments

f

A function that combines two values (such as two array elements) and returns a new “reduced” value.

initial

An optional initial value to seed the array reduction with. If this argument is specified, reduce() behaves as if it had been inserted at the start of array.

Returns

The reduced value of the array, which is the return value of the last invocation of f.

Description

The reduce() method expects a function f as its first argument. This function should behave like a binary operator: it takes two values, performs some operation on them and returns a result. If array has n elements, the reduce() method invokes f n-1 times to reduce those elements to a single combined value. (You may be familiar with this array reduction operation from other programming languages where it is sometimes called “fold” or “inject”.)

The first invocation of fis passed the first two elements of array. Each subsequent invocation of f is passed the return value of the previous invocation and the next element (in ascending order) of array. The return value of the final invocation of f becomes the return value of the reduce() method itself.

reduce() may be invoked with an optional second argument, initial. If initial is specified, then reduce() behaves as if that argument has been inserted at the beginning of array (it does not actually modify array, however). Another way to say this is that if reduce() is invoked with two arguments, then initial is used as if it had previously been returned from f. In this

Array.reduceRight()

case, the first invocation of f is passed initial and the first element of array. When initial is specified, there are n+1elements to reduce (nelements of array, plus the initialvalue) and f is invoked n times.

If array is empty and initial is not specified, reduce() throws a TypeError. If array is empty and initial is specified, then reduce() returns initial and never calls f. If array has only a single element and initial is not specified, then reduce() returns the single element of array without calling f.

The paragraphs above describe two arguments to f, but reduce() actually invokes that function with four arguments. The third argument is the array index of the second argument. The fourth argument is array itself. f is always invoked as a function, not as a method.

Example

[1,2,3,4].reduce(function(x,y) { return x*y; }) // => 24: ((1*2)*3)*4

See Also

Array.forEach() , Array.map() , Array.reduceRight()

Array.reduceRight() ECMAScript 5

reduce an array from right-to-left

Synopsis

array.reduceRight(f) array.reduceRight(f, initial)

Arguments

f

A function that combines two values (such as two array elements) and returns a new “reduced” value.

initial

An optional initial value to seed the array reduction with. If this argument is specified, reduceRight() behaves as if it had been inserted at the end of array.

Returns

The reduced value of the array, which is the return value of the last invocation of f.

Description

reduceRight() works like the reduce() method: it invokes the function f n-1 times to reduce the n elements of array to a single value. reduceRight() differs from reduce() only in that it enumerates array elements from right to left (from highest index to lowest) rather than left to right. See Array.reduce() for details.

Example

[2, 10, 60].reduceRight(function(x,y) { return x/y }) // => 3: (60/10)/2

Core JavaScript Reference | 733

Array.reverse()

See Also

Array.reduce()

Array.reverse()

reverse the elements of an array

Synopsis

array.reverse()

Description

The reverse() method of an Array object reverses the order of the elements of an array. It does this in place: it rearranges the elements of the specified array without creating a new array. If there are multiple references to array, the new order of the array elements is visible through all references.

Example

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