javascript 函数中不同类型的参数
different types of parameters in javascript function
我对 javascript 很陌生。通过 javascript 代码时,我发现 ::
function Method1(sender, args) { ... }
function Method2(source, arguments) { ... }
何时使用 (sender, args) 和 (source, arguments)?他们的意思是什么?
arguments
实际上不是 JS reserved word,否则你的 Method2
将无法工作并且会抛出语法错误。
当函数的参数列表中使用 arguments
时,它只是一个常规函数参数/参数,与您的 sender
、source
和 [=15= 完全一样].
现在 Arguments
不能用作 class 名称是真的。 JavaScript 在内部使用它来创建一个 arguments
object(即 Tushar 在评论中提到的那个)。
When control enters an execution context for function code, an arguments object is created unless (as specified in 10.5) the identifier arguments occurs as an Identifier in the function’s FormalParameterList or occurs as the Identifier of a VariableDeclaration or FunctionDeclaration contained in the function code.
在每个函数中,您可以使用此 arguments
类数组对象访问在该函数调用期间传递的所有参数。这对于可能接受非预定(并且可能无限)数量的参数的函数非常有用。
所以您的 Method2
会发生什么,它使用相同的标识符 "arguments",现在只分配了一个参数。就好像它 隐藏了 内置的 arguments
对象。
而如果没有在参数列表中使用,功能块中的 arguments
将自动分配所有参数的列表。
演示:http://jsfiddle.net/5cexbrff/(在控制台中查看结果)
我对 javascript 很陌生。通过 javascript 代码时,我发现 ::
function Method1(sender, args) { ... }
function Method2(source, arguments) { ... }
何时使用 (sender, args) 和 (source, arguments)?他们的意思是什么?
arguments
实际上不是 JS reserved word,否则你的 Method2
将无法工作并且会抛出语法错误。
当函数的参数列表中使用 arguments
时,它只是一个常规函数参数/参数,与您的 sender
、source
和 [=15= 完全一样].
现在 Arguments
不能用作 class 名称是真的。 JavaScript 在内部使用它来创建一个 arguments
object(即 Tushar 在评论中提到的那个)。
When control enters an execution context for function code, an arguments object is created unless (as specified in 10.5) the identifier arguments occurs as an Identifier in the function’s FormalParameterList or occurs as the Identifier of a VariableDeclaration or FunctionDeclaration contained in the function code.
在每个函数中,您可以使用此 arguments
类数组对象访问在该函数调用期间传递的所有参数。这对于可能接受非预定(并且可能无限)数量的参数的函数非常有用。
所以您的 Method2
会发生什么,它使用相同的标识符 "arguments",现在只分配了一个参数。就好像它 隐藏了 内置的 arguments
对象。
而如果没有在参数列表中使用,功能块中的 arguments
将自动分配所有参数的列表。
演示:http://jsfiddle.net/5cexbrff/(在控制台中查看结果)