伪数组转数组,apply&slice方法将类似数组的对象转换成数组

    技术2026-02-08  2

    apply&slice方法将类似数组的对象转换成数组

    // 利用数组对象的slice方法,可以将一个类似数组的对象(比如arguments对象)转为真正的数组。 Array.prototype.slice.apply({0: 1, length: 1}) // [1] Array.prototype.slice.apply({0: 1}) // [] Array.prototype.slice.apply({0: 1, length: 2}) // [1, undefined] Array.prototype.slice.apply({length: 1}) // [undefined] [1, 2, 3].slice(0, 1) // [1] // 等同于 Array.prototype.slice.call([1, 2, 3], 0, 1) // [1] var push = Function.prototype.call.bind(Array.prototype.push); var pop = Function.prototype.call.bind(Array.prototype.pop); var a = [1 ,2 ,3]; push(a, 4) a // [1, 2, 3, 4] pop(a) a // [1, 2, 3]
    Processed: 0.022, SQL: 9