arguments 是一个 对应于 传递给函数的参数 的 类数组(array-like)对象

  • array-like意味着它不是一个数组类型,而是一个对象类型

    • 但是它却拥有数组的一些特性,比如说length,比如可以通过index索引来访问
    • 但是它却没有数组的一些方法,比如filter、map等;
    • // 函数中默认带有arguments变量 是一个类数组形式//  1.可以接收用户传递过来的所有实参function foo(a,b){console.log(arguments);//2.通过索引值获取数组中的内容console.log(arguments[0]);console.log(arguments[1]);console.log(arguments[2]);//3.使用for循环  for of遍历数组for (var i of arguments){console.log(i);}}foo(10,20,30)

arguments转Array

  • 在开发中,我们经常需要将arguments转成Array,以便使用数组的一些特性

    • 遍历arguments,添加到一个新数组中;
    • Array.from
    • […arguments]
    • // 函数中默认带有arguments变量 是一个类数组形式function foo(a,b){const newArr =[];// for循环遍历追加for(let i=0;i<arguments.length;i++){newArr.push(arguments[i])}console.log(newArr);// Arry.form(可迭代的伪数组)let xinshuzu = Array.from(arguments);console.log(xinshuzu);// [...arguments] 扩展运算符let xinshuzu2 = [...arguments];console.log(xinshuzu2);}foo(10,20,30,40)

箭头函数没有arguments

  • 箭头函数是不绑定arguments的,所以我们在箭头函数中使用arguments会去上层作用域查找
  • // 箭头函数没有argumentslet foo = ()=>{console.log(arguments);}// f00()//报错// 箭头函数没有arguments就会访问上一层作用域查找function getfoo(){let foo = ()=>{console.log(arguments);}foo()}getfoo(10,20,30)

函数的剩余(rest)参

  • ES6中引用了rest parameter,可以将不定数量的参数放入到一个数组中:

    • 如果最后一个参数是 ... 为前缀的,那么它会将剩余的参数放到该参数中,并且作为一个数组;

  • 那么剩余参数和arguments有什么区别呢?
    • arguments对象不是一个真正的数组,而rest参数是一个真正的数组,可以进行数组的所有操作;
    • arguments是早期的ECMAScript中为了方便去获取所有的参数提供的一个数据结构,而rest参数是ES6中提供并且希望以此 来替代arguments的;
  • 剩余参数必须放到最后一个位置,否则会报错。
    • //1. 剩余参数 就是形参  一般都是以 ... 开头的//2. 剩余参数 是数组的形式  因为100给了a 200给了b //              剩余全部参数放到x中//3. 剩余参数一定要放到函数的末尾  否则会报错//4. 函数的形参可以一个不写  只写一个剩余形参  那么这个剩余形参会接收所有实参//5. 现在这个剩余参数就是类似于我们的arguments//              区别 arguments是伪数组  剩余参数是真正的数组//箭头函数没有arguments 但是有剩余参数function foo(a,b,...c){// console.log(arguments)console.log(a,b,c);}foo(100,200,300,400,500,600)function bar(...newArg){console.log(newArg);}bar("张三","李四","王五","赵六");var fn = (...arg)=>{console.log(arg);}fn(10,20,30);

认识arguments相关推荐

  1. JavaScript arguments对象

    1.在JavaScript中,arguments对象是比较特别的一个对象,实际上是当前函数的一个内置属性.arguments非常类似Array,但实际上又不是一个Array实例.可以通过如下代码得以证 ...

  2. 【Qt】Qt再学习(十六):QObject::connect: Cannot queue arguments of type ‘QString‘

    1.问题描述 跨线程使用信号和槽时,如果是非const的引用传参,就会报如下的错误: QObject::connect: Cannot queue arguments of type 'QString ...

  3. java unlimited_具有无限参数的Java方法(Java method with unlimited arguments)

    具有无限参数的Java方法(Java method with unlimited arguments) Spring框架使用方法,您可以根据需要传递尽可能多的参数. 我想写一个函数,也可以采取无限量的 ...

  4. javascript函数嵌套时arguments的问题

    疑问: var funtest = function () {var fun = function (val, val2) {alert(arguments.length); //此处答案? 有些人回 ...

  5. IDE set arguments

    2019独角兽企业重金招聘Python工程师标准>>> code::blocks -> Project -->set program's arguments qtcrea ...

  6. 解决Undefined function or method 'vgg_kmiter' for input arguments of type 'double'.

    Undefined function or method 'vgg_kmiter' for input arguments of type 'double'. Error in ==> vgg_ ...

  7. 浅析js中的arguments

    arguments就是传递进函数的参数列表,它是一个类数组对象-Array-Like Object. 类数组对象,简单来说就是拥有length属性,如我们常用的NodeList,arguments,但 ...

  8. Error in select(., cyl, mpg) : unused arguments (cyl, mpg)

    Error in select(., cyl, mpg) : unused arguments (cyl, mpg) 目录 Error in select(., cyl, mpg) : unused ...

  9. oracle ora 13011,ORA-00600: internal error code, arguments: [13011]

    近期,一客户历史数据库屡次遇到ORA-00600: internal error code, arguments: [13011].附带的,还有ORA-01499 SQL> analyze ta ...

  10. python调用dll报错:ValueError: Procedure called with not enough arguments (4 bytes missing) or wrong call

    python调用dll报错:ValueError: Procedure called with not enough arguments (4 bytes missing) or wrong call ...

最新文章

  1. 开始研究JavaScript
  2. yarn add webpack webpack-cli 报错
  3. 前端开发 常用用的静态服务器
  4. python入门--基本语法
  5. merlin.acs的使用方法 merlin.acs添加右键菜单
  6. playframe 项目搭建
  7. python默认参数的传参方式_如何跳过在Python方法中提供默认参数
  8. 上下文路径request.getContextPath();与${pageContext.request.contextPath}
  9. VB程序设计算机,VB编程:编写一个过程,用来计算并输出 S=1+1/2+1/3+……+1/100 的值。...
  10. 移动端H5解惑-页面适配
  11. PLC+智能网关,实现HTTP+JSON/XML+POST/GET请求与解析
  12. 毕业设计 - 基于JAVA的小区/园区停车管理系统(简便易上手)
  13. python 实现一个属于自己的语音播报器
  14. no moudle named dlib
  15. 云搜网盘助手域名更换公告
  16. 618最强攻略揭秘:成为网易考拉的黑卡会员!
  17. open3d,读取stl/ply/obj/off/gltf/glb三维模型,并转换成点云,保存
  18. [ Linux ] PCF8563数据手册解析 |CSDN创作打卡
  19. 神经网络与傅立叶变换到底有没有关系?
  20. oCPC实践录 | 糟糕,广告主成本超了!

热门文章

  1. Tensorflow下用自己的数据集对Faster RCNN进行训练和测试(二)1
  2. 使用css做一个右向的三角箭头
  3. 百度API—身份证查询
  4. 在try-catch机制优化IO流关闭时,OutputStreamWriter 数据流被截断 新语法
  5. 数据库—查询语句中asc、desc表示什么意思?
  6. salesforce 定时任务遇到的坑
  7. pytorch并行处理详解(多GPU,环境变量)
  8. 计算机教 学计划,教师计算机学习计划(共3篇)
  9. 空间里相片批量导入u盘_如何将手机中的照片、视频快速的保存到U盘上?3分钟教你详细步骤...
  10. Androidstudio报错问题之R飘红