最近在看JavaScript相关的知识点,看到了老外的一本Javascript For Web Developers,遇到了一个知识盲点,以为老外写的很明白很透彻,记录下来加深印象,下面是我摘出来的一些片断,片断下有对应的解释,但愿也能帮助其余人扫除这个盲点。若有翻译的不得体的地方还请在评论区指出,不胜感激。html
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对应的值。app
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:ide
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
属性来检查传入的参数的长度,下面的代码展现了每一个函数被调用时传入参数的个数:this
function howManyArgs() { console.log(arguments.length); } howManyArgs("string", 45); //2 howManyArgs(); //0 howManyArgs(12); //1
对我来讲,之因此这里是一个知识盲点,或多或少与思惟定势有些关系,之前老是认为function的参数和强类型语言是同样的,怎么定义的就怎么传递参数。可是,命名参数仍是有好处的,不用经过arguments索引方式获取参数值。总之,扫除了一个知识盲点。spa
做者:悠扬的牧笛翻译
博客地址:http://www.cnblogs.com/xhb-bky-blog/p/9361395.html code
声明:本博客原创文字只表明本人工做中在某一时间内总结的观点或结论,与本人所在单位没有直接利益关系。非商业,未受权贴子请以现状保留,转载时必须保留此段声明,且在文章页面明显位置给出原文链接。若是您以为文章对您有帮助,能够【打赏】博主或点击文章右下角【推荐】一下。您的鼓励是博主坚持原创和持续写做的最大动力!