首页  >  问答  >  正文

javascript - Array.prototype.slice.call(arguments, 1) 不就是等于 arguments.slice(1)?

这是jqFloat插件里的代码:

if (element.data('jDefined')) {
    if (options && typeof options === 'object') {
        methods.update.apply(this, Array.prototype.slice.call(arguments, 1));
    }
} else {
    methods.init.apply(this, Array.prototype.slice.call(arguments, 1));
}

多次用到 Array.prototype.slice.call(arguments, 1),不就是等于 arguments.slice(1) 吗?像前者那样写具体的好处是什么?

PHP中文网PHP中文网2748 天前494

全部回复(2)我来回复

  • 天蓬老师

    天蓬老师2017-04-10 14:23:27

    因为arguments并不是真正的数组对象,只是与数组类似而已,所以它并没有slice这个方法,
    而Array.prototype.slice.call(arguments, 1)可以理解成是让arguments转换成一个数组对象,让arguments具有slice()方法。
    要是直接写arguments.slice(1)会报错

    回复
    0
  • 迷茫

    迷茫2017-04-10 14:23:27

    Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组;

    var a={length:3,0:'abc',1:'def',2:'ghi'};
    console.log(Array.prototype.slice.call(a));//  ["abc", "def",'ghi']
    var a={length:0};
    console.log(Array.prototype.slice.call(a));//  []
    var a={length:3};
    console.log(Array.prototype.slice.call(a));//  [undefined, undefined, undefined]
    

    回复
    0
  • 取消回复