[Js] some tips (1), usage of Array.prototype.push, Array.prototype.slice of

When reading the source code will see this operation:

1.Array.prototype.push Introduction

var push = Array.prototype.push;

push.apply(args, arguments);

Why use push.apply, rather than directly push it?

//push.apply

var a = [1,2,3] , b = [4,5,6],push = Array.prototype.push;
push.apply(a,b) ;
console.log(a)  // [1, 2, 3, 4, 5, 6]

//push

var a = [1,2,3] , b = [4,5,6];
a.push(b)
console.log(a)  // [1, 2, 3, Array(3)]

array push method receiving a return parameter list, it does not automatically extend to a parameter list array,

Use apply the wording may be extended array parameters into the parameter list, so that the two arrays can be combined directly pass array parameters.

2.Array.prototype.slice Introduction

A typical array of classes: arguments,

A function (A, B) {
var args = arguments;
the console.log (args.length); // Although length, but the method of operation of the array being given
console.log (Array.prototype.slice.apply (arguments)) // [. 1, 2]
}
A (1,2);

Array.prototype.slice.apply () may be based array into an array, then the array method of using the built-in

 

Guess you like

Origin www.cnblogs.com/zhangqian1/p/12043735.html