Goodsu

导航

 

 

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用于实现类似于继承的功能、稍后补充

 

posted on 2015-08-07 21:43  Goodsu  阅读(157)  评论(0编辑  收藏  举报