浅析js中的arguments

news/2024/6/30 10:34:32

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

1. length

var foo = function () {console.log(arguments.length)
}
foo() // 0
foo(1, 2) // 2

2. Function.arguments永远不要使用,已被废除,直接使用arguemnts对象

3. arguments对象会被参数或变量arguments覆盖

var foo = function () {console.log(arguments) // [10]
}
foo(10)var bar = function (arguments) {console.log(arguments) // 10
}
bar(10)var foo = function () {console.log(arguments) // [10]
}
foo(10)
var bar = function (arguments) {arguments = 20console.log(arguments) // 20
}
bar(20)

4.将arguments对象转化为数组对象

  • arguments = [].slice.call(arguments)

  • arguments = Array.from(arguments)

  • 待补充(暂时没想到其余简洁的写法.)

5. arguments.callee指向当前正在被执行的函数 -- 应当停止使用

最常见的一个例子
阶乘

var foo = function (x) {return x >= 1 ? x * arguments.callee(x-1) : 1
}
console.log(foo(3)) // 6

关于它更详尽的解释,我更愿意给你推荐下面这个链接。
link-可能需要自备梯子
值得注意的一点是,在下面我们提到的ES5 use strict模式下,使用arguments.callee会报错.

5. arguments数量 >= parameters数量 -- 双向绑定

var foo = function (x, y) {arguments[0] = 10console.log(arguments[0], x) //10, 10x = 20console.log(arguments[0], x) //20, 20arguments[1] = 30console.log(arguments[1], y) //30, 30y = 40console.log(arguments[1], y) //40, 40
}
foo(1, 2)

6. arguments数量 < parameters数量 -- 缺省参数未双向绑定

var foo = function (x, y) {arguments[0] = 10console.log(arguments[0], x) //10, 10x = 20console.log(arguments[0], x) //20, 20arguments[1] = 30console.log(arguments[1], y) //30, undefinedy = 40console.log(arguments[1], y) //40, 40
}
foo(1)

7. use strict

以上2种情况,会给我们在使用时稍不注意,不去区分,就会犯错。
不过还好,我们可以通过ES 5use strict特性进行统一.

//对应上面第一种情况
var foo = function (x, y) {'use strict'arguments[0] = 10console.log(arguments[0], x) //10, 1x = 20console.log(arguments[0], x) //10, 20arguments[1] = 30console.log(arguments[1], y) //30, 2y = 40console.log(arguments[1], y) //30, 40
}
foo(1, 2)
//对应上面第二种情况
var foo = function (x, y) {'use strict'arguments[0] = 10console.log(arguments[0], x) //10, 1x = 20console.log(arguments[0], x) //10, 20arguments[1] = 30console.log(arguments[1], y) //30, undefinedy = 40console.log(arguments[1], y) //30, 40
}
foo(1)

因为在严格模式下,arguments对象被禁止为函数参数创建gettersetter方法。

8. 使用...Rest代替

...Rest只能被定义在函数参数的最后一个位置,传进函数的多余参数都将传进这个数组

var foo = function (x, y, ...z) {console.log(z) // [3, 4, 5]console.log(Array.isArray(z)) // true
}
foo(1, 2, 3, 4, 5)

结束语

欢迎指正错误和提出更多的关于`arguments`的内容~

http://lihuaxi.xjx100.cn/news/282199.html

相关文章

XML中 元素和属性比较

在XML中&#xff0c;并没有规定何时使用属性&#xff0c;以及何时使用子元素。使用元素 vs. 属性 数据可以存储在子元素或属性。 让我们来看下这些实例: <person sex"female"> <firstname>Anna</firstname> <lastname>Smith</lastname…

开源机器学习库清单

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达最近我们发现了一些非常有趣的开源机器学习库并把它们列成了一个清单&#xff0c;今天就一起来分享以下吧。01.DeOldify这个开源项目主要是将灰度图像转换成色彩图像&…

php linux脚本文件,Unix/Linux中如何直接执行PHP脚本文件?

使用Linux系统的服务器都有搭建完整的PHP环境&#xff0c;因此有些用户会用PHP去写一些执行自动化任务的脚本&#xff0c;可是发现每次执行PHP脚本都需要使用php myscript.php的方式&#xff0c;感觉有点麻烦。其实我们是可以直接执行PHP脚本文件的&#xff0c;但是具体该怎么操…

从0到1 | 手把手教你如何使用哈工大NLP工具——PyLTP!

作者 | 杨秀璋来源 | CSDN 博客&#xff08;CSDN id&#xff1a;Eastmount&#xff09;&#xff08;本文经作者授权&#xff0c;此系列文章整理后微信平台首发于AI科技大本营&#xff09;【导语】此文是作者基于 Python 构建知识图谱的系列实践教程&#xff0c;具有一定创新性和…

转载:用 Tomcat 和 Eclipse 开发 Web 应用程序

原文地址:http://www.ibm.com/developerworks/cn/opensource/os-eclipse-tomcat/所需的组件 Eclipse V3.2 Callisto 集成开发环境 (IDE) 包括了用于 Web 开发及与服务器集成的工具。所以&#xff0c;除了软件开发工具箱 (SDK) 之外&#xff0c;只需安装 Eclipse 和 Apache Tomc…

因为没有数学,就没有现在的计算机科学。所以,请务必学好数学!

数学在计算机科学中的重要性数学是一门工具性很强的科学&#xff0c;它与别的科学比较起来还具有较高的抽象性等特征。起初是计算机科学工作者离不开数学&#xff0c;而数学工作者认为计算机对他们可有可无&#xff0c;但是现在是互相都离不开对方了&#xff0c;计算机也提高了…

我们一起来玩转 Grep 指令

grep这个linux指令大家一定不陌生&#xff0c;其用于查找文件中符合条件的字符串&#xff0c;下面来看看这个高频的指令如何使用。在一个阳光明媚、晴空万里的中午&#xff0c;一个挠头的程序员正在与团队一姐排查超时问题&#xff0c;只见一姐手速极快的查找着一个又一个日志&…