码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-10-12 17:46:01      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:参数转换   rgs   ret   turn   转换   cti   ||   ons   prot   

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

  • 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()为什么能将类数组转换为数组

标签:参数转换   rgs   ret   turn   转换   cti   ||   ons   prot   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!