前言
最近在看JavaScript相关的知识点,看到了老外的一本Javascript For Web Developers,遇到了一个知识盲点,觉得老外写的很明白很透彻,记录下来加深印象,下面是我摘出来的一些片段,片段下有对应的解释,希望也能帮助其他人扫除这个盲点。如有翻译的不得体的地方还请在评论区指出,不胜感激。
理解Javascript中的Arguments
Function
arguments
in ECMAScript don’t behave in the same way as function arguments in most other languages. An ECMAScript function doesn’t care how many arguments are passed in, nor does it care about the data types of those arguments. Just because you define a function to accept two arguments doesn’t mean you can pass in only two arguments. You could pass in one or three or none, and the interpreter won’t complain. This happens because arguments in ECMAScript are represented as an array internally. The array is always passed to the function, but the function doesn’t care what (if anything) is in the array. If the array arrives with zero items, that’s fine; if it arrives with more, that’ s okay too. In fact,there actually is an arguments object that can be accessed while inside a function to retrieve the values of each argument that was passed in.
Function arguments在ECMAScript中的行为并不像其他大多数语言中的函数参数。在ECMAScript中,function并不关心有多少个参数传入函数中,也不关心传入参数的数据类型。当你定义了一个有两个参数的函数时,并不意味着你一定要传递两个参数,你也可以传入一个或者三个甚至是不传,这并不影响函数的解释。发生这种现象的原因是在ECMAScript中的arguments代表的是一个内部的array,这个array有0个元素是可以的,包含多个元素也是可以的。实际上,在ECMAScript中有一个arguments对象,当function有参数传入的时候,可以根据索引去获取这个arguments对应的值。
The
arguments
object acts like an array (though it isn’t an instance ofArray
) in that you can access each argument using bracket notation (the first argument isarguments[0]
, the second isarguments[1]
,and so on) and determine how many arguments were passed in by using thelength
property. In theprevious example, thesayHi()
function’s first argument is namedname
. The same value can be accessed by referencingarguments[0]
. Therefore, the function can be rewritten without naming the arguments explicitly, like this:
arguments
对象的行为有些类似于array
,但是实际上它并不是Array
的实例,在arguments
中,可以通过索引的方式获取对应的值,例如第一个参数是arguments[0]
,第二个参数是arguments[1]
等等,并且可以根据arguments
的length
属性获取传入的参数的数量。在之前的例子中,sayHi()
函数的第一个参数命名为name
,与之相同的值可以通过arguments[0]
来获取。因此,function可以写成没有参数的形式,像这样:
function sayHi() { console.log("Hello" + arguments[0] + "," + arguments[1]); } SayHi("Jim","Have a good day!");
In this rewritten version of the function, there are no named arguments. The name and message arguments have been removed, yet the function will behave appropriately. This illustrates an important point about functions in ECMAScript: named arguments are a convenience, not a necessity. Unlike in other languages, naming your arguments in ECMAScript does not create a function signature that must be matched later on; there is no validation against named arguments.The arguments object can also be used to check the number of arguments passed into the function via the length property. The following example outputs the number of arguments passed into the function each time it is called:
在这个版本的代码中,并没有被命名的参数,name
和message
参数被移除了,但是函数依然会正常执行,这依赖于ECMAScript重要的特性,参数的命名只是为了方便,并不是必须的。不像其他的语言,定义的函数命名了参数并不一定非要编写与之签名一致的函数,也没有强制验证命名参数。arguments
对象还可以通过length
属性来检查传入的参数的长度,下面的代码展示了每个函数被调用时传入参数的个数:
function howManyArgs() { console.log(arguments.length); } howManyArgs("string", 45); //2 howManyArgs(); //0 howManyArgs(12); //1
对我来说,之所以这里是一个知识盲点,或多或少与思维定势有些关系,以前总是认为function的参数和强类型语言是一样的,怎么定义的就怎么传递参数。但是,命名参数还是有好处的,不用通过arguments索引方式获取参数值。总之,扫除了一个知识盲点。
作者:悠扬的牧笛
博客地址:http://www.cnblogs.com/xhb-bky-blog/p/9361395.html
声明:本博客原创文字只代表本人工作中在某一时间内总结的观点或结论,与本人所在单位没有直接利益关系。非商业,未授权贴子请以现状保留,转载时必须保留此段声明,且在文章页面明显位置给出原文连接。如果您觉得文章对您有帮助,可以【打赏】博主或点击文章右下角【推荐】一下。您的鼓励是博主坚持原创和持续写作的最大动力!
原文地址:https://www.cnblogs.com/xhb-bky-blog/p/9361395.html