首页 > 其他 > 详细

Array.prototype.slice.call(arguments)

时间:2015-08-07 23:31:43      阅读:360      评论:0      收藏:0      [点我收藏+]

 

Array.prototype.slice.call(arguments)

话说可以通过call()来让arguments来继承array的slice()方法。

可是为何使用arguments.slice(1)会报错,

而这样使用Array.prototype.slice.call(arguments,1)就可以正常。

 

function name(){
Array.prototype.slice.call(arguments);
console.log(arguments.slice(1));
}
name("some","thing");

结果:TypeError: arguments.slice is not a function

 

function name(){
console.log(Array.prototype.slice.call(arguments,1));
}
name("some","thing");

结果为: ["thing"]

 

mdn给出的把arguments转换成array对象时,做了一个赋值:var args = Array.prototype.slice.call(arguments);

function name(){
var arg=Array.prototype.slice.call(arguments);
console.log(arg instanceof Array);
console.log(arg.slice(1));
}
name("some","thing");

结果是: true
            ["thing"]

所以arg在这里才是arguments转换知之后的array对象。

那我们需要研究一下call()返回了什么?

 

 

//备注

array.slice();用于选出数组中制定元素,返回一个新的数组对象。

call();关于call用于实现类似于继承的功能、稍后补充

 

Array.prototype.slice.call(arguments)

原文:http://www.cnblogs.com/web-coding/p/4711982.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!