Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)

Array.prototype.push.apply(a,b)

时常看到在操作数组的时候有这样的写法:

var a = [1,2,3];
var b = [4,5,6];

a.push.apply(a, b);

console.log(a) //[1,2,3,4,5,6]

其实这样的写法等价于:
var a = [1,2,3];
var b = [4,5,6];

Array.prototype.push.apply(a, b);

console.log(a) //[1,2,3,4,5,6]

这样写法等价的原因是因为在实例上寻找属性的时候,现在这个实例自己身上找,如果找不到,就根据内部指针__proto__随着原型链往上找,直到找到这个属性。

在这里就是寻找push方法,两种写法最后找到的都是Array构造函数对应的prototype的原生方法push。所以说两种写法是等价的。

但是为什么要使用a.push.apply(a,b);这种写法呢?为什么不直接使用push()?

如果直接push:

var a = [1,2,3];
var b = [4,5,6];

a.push(b);

console.log(a) //[1, 2, 3, Array(3)]
 

这样就看出来区别了,原生push方法接受的参数是一个参数列表,它不会自动把数组扩展成参数列表,使用apply的写法可以将数组型参数扩展成参数列表,这样合并两个数组就可以直接传数组参数了。

但是合并数组为什么不直接使用Array.prototype.concat()呢?

因为concat不会改变原数组,concat会返回新数组,而上面apply这种写法直接改变数组a。

同理,Math.max和Math.min也可以使用apply这种写法来传入数组参数。

比如这样:

Math.max.apply(null,a)
 

这样就可以很方便的传入数组参数了。

Array.prototype.slice.call(arguments)

类似的还有这样的写法,MDN解释slice方法可以用来将一个类数组(Array-like)对象/集合转换成一个新数组。你只需将该方法绑定到这个对象上。

所以可以使用slice将函数的参数变成一个数组,然后就可以当做数组来操作了。

 

原文地址:https://www.cnblogs.com/zhx119/p/10325918.html

时间: 2024-08-27 02:12:45

Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)的相关文章

Array.prototype.push.apply

Array.prototype.push.apply(a,b) || a.push.apply(a,b);var a=[1,2,3],b=[4,5,6];Array.prototype.push.apply(a,b) a===[1, 2, 3, 4, 5, 6]b===[4, 5, 6] http://blog.csdn.net/maoguiyou/article/details/51433551

Array,prototype.concat.apply与[].conat.apply.

一直都知道JS数组Array内置对象有一个concat方法,但是也没怎么研究过,今天偶然就看了看 concat是连接一个或多个数组 返回的是连接后数组的一个副本 var oldArr=[]; var arr=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]; var newArr=oldArr.conat(arr); console.log(newArr); console.log(oldArr);//[]  没有改变 于是乎 我又想到把数组的每一项都当成数组与oldAr

【js】一些小技巧(1),关于Array.prototype.push,Array.prototype.slice的用法

阅读源码的时候会看到这样操作: 1.Array.prototype.push的介绍 var push = Array.prototype.push; push.apply(args, arguments); 为什么会用push.apply,而不是直接push呢? //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,

JavaScript,通过分析Array.prototype.push重新认识Array

在阅读ECMAScript的文档的时候,有注意到它说,数组的push方法其实不仅限于在数组中使用,专门留作通用方法.难道是说,在一些类数组的地方也可以使用?而哪些是和数组非常相像的呢,大家或许一下子就可以想到就是Object对象.因为Array就是继承自Object的,可以用 [] instanceof Object,会发现返回的是true.当然大家都知道,这也不是什么新鲜事.那我们可以大胆尝试一下,如果我们将数组的push方法应用在对象上,会一个怎么样的表现呢? 我们通过call,将this的

理解Array.prototype.slice.call(arguments)

在很多时候经常看到Array.prototype.slice.call()方法,比如Array.prototype.slice.call(arguments),下面讲一下其原理: 1.基本讲解 1.在js里Array是一个类 slice是此类里的一个方法 ,那么使用此方法应该Array.prototype.slice这么去用 slice从字面上的意思很容易理解就是截取(当然你不是英肓的话) 这方法如何使用呢? arrayObj.slice(start, [end]) 很显然是截取数组的一部分.

es6 class的基础语法,es6 class继承/ es5 call继承描述/使用apply比较完美继承Array的方法 sort倒序排序console.table()表格生成

//基础语法function People(name,email){ name, email } class User{ constructor(name,email){ this.name = name; this.email = email; } getinfo(){ console.log(this.name); } static description(){ console.log('I am description --static'); } set urls(values){ con

将函数的实际参数转换成数组的方法,习惯用Array.prototype.slice.call(arguments)

实际参数在函数中我们可以使用 arguments 对象获得 (注:形参可通过 arguments.callee 获得),虽然 arguments 对象与数组形似,但仍不是真正意义上的数组. 我们可以通过数组的 slice 方法将 arguments 对象转换成真正的数组. 方法一:通过Array.prototype属性调用slice方法 var args = Array.prototype.slice.call(arguments); Array 本身是没有 slice 方法,它的方法在 Arr

伪数组转为数组 Array.prototype.slice.call(arguments)

我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js对象与com对象不能进行转换) 1 var a={length:2,0:'first',1:'second'}; 2 Array.prototype.slice.call(a);// ["first", "second"] 3 4 var a={length:2}; 5

浅谈Array.prototype.slice.call(arguments,0)

在谈论这个问题之前 我们先了解下call的用法及作用 对象.方法.call(obj[,arg1[, arg2[,   [,.argN]]]]]):调用一个对象的一个方法,以另一个对象替换当前对象. 1 function Personal(a){ 2 3 this.name = a; 4 5 this.say = function(){ 6 7 alert(a) 8 } 9 10 } 11 12 function Man(a){ 13 14 this.sex = "maie"; 15 P