首页 > 编程语言 > 详细

Array.prototype.slice.call()为什么能将类数组转换为数组

时间:2018-10-12 17:28:57      阅读:173      评论:0      收藏:0      [点我收藏+]

将类数组转换为数组的方法

  • Array.prototype.slice.call(arguments)
  • [].slice.call(arguments)
  • Array.from(arguments)
function turnToArray(){
    var arrArgs = Array.prototype.slice.call(arguments) // 将参数转换成数组
    console.log.apply(console,arguments) // a b(打印出所有参数 )
    console.log(arrArgs) // [‘a‘,‘b‘]
}
turnToArray(‘a‘,‘b‘)
  • Array.prototype.slice.call(arguments) 将类数组转换为数组的原理
    关键是数组对象上的slice方法

function slice(start, end) { 
    var startToUse = start || 0, 
        endToUse = end || ToUint32(this.length), 
        result = []; 
    for(var i = startToUse; i < endToUse; i++) { 
        result.push(this[i]); 
    }
    return result; 

Array.prototype.slice.call()为什么能将类数组转换为数组

原文:https://www.cnblogs.com/huangxingyuan/p/9779285.html

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