js post 重复参数
Assertion: We are in parallel universe where all weird code snippets may exist and be used.
断言 :我们处于并行宇宙中,所有奇怪的代码片段都可能存在并被使用。
Let’s imagine the simplest function declaration with 2 parameters… but both parameters have the same name:
让我们想象一下最简单的带有2个参数的函数声明…,但是两个参数具有相同的名称:
function f(a,a){ console.log(a)}What reaction would you expect from JS?
您对JS有什么React?
Probably there are 2 options:1) throw some error (we are lucky if it’s not runtime error);2) create valid function f
大概有2个选项:1)抛出一些错误(如果不是运行时错误,我们很幸运); 2)创建有效的函数f
Of course we may just run the code and check but it is too simple.Instead, I propose to find the origin of truth and open ES specification 📕
当然,我们可能只运行代码并进行检查,但这太简单了,我建议找到真相的起源并开放ES规范 📕
As in the snippet we try to create a function, so go to Function Definitions section in the spec. Inside we may find the following:
就像在代码段中一样,我们尝试创建一个函数,因此请转到规范中的“ 函数定义”部分。 在内部,我们可能会发现以下内容:
FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody }FunctionExpression : function BindingIdentifieropt ( FormalParameters ) { FunctionBody }
FunctionDeclaration: 函数 BindingIdentifier(FormalParameters){FunctionBody} FunctionExpression: 函数 BindingIdentifieropt(FormalParameters){FunctionBody}
If the source code matching this production is strict code, the Early Error rules for StrictFormalParameters : FormalParameters are applied.
如果与此产品匹配的源代码是严格代码,则将应用 StrictFormalParameters : FormalParameters 的Early Error规则 。
It means if you try to create a function as function declaration or function expression in "strict mode" some additional Early Error(errors on code parsing stage) rule is applied:
这意味着,如果您尝试在"strict mode"将函数创建为函数声明或函数表达式,则会应用一些附加的Early Error(代码解析阶段的错误)规则:
StrictFormalParameters : FormalParameters
StrictFormalParameters:FormalParameters
It is a Syntax Error if BoundNames of FormalParameters contains any duplicate elements.
如果FormalParameters的 BoundNames 包含任何重复的元素 ,则是语法错误 。
In that context BoundNames are just parameter names.
在这种情况下,BoundNames只是参数名称。
So the spec says that if you try to create a function as function declaration or function expression in "strict mode" and use the same name for function parameters then JS returns Syntax Error!
因此,规范说明,如果您尝试在"strict mode"将函数创建为函数声明或函数表达式,并对函数参数使用相同的名称,则JS返回语法错误!
function f(a,a){ 'use strict' console.log(a)}Just copy and paste the above snippet in your browser console and check the error:
只需将以上代码段复制并粘贴到浏览器控制台中,然后检查错误:
Uncaught SyntaxError: Duplicate parameter name not allowed in this context
Uncaught SyntaxError: Duplicate parameter name not allowed in this context
Rather obviously, right?
显然是吧?
In non-strict mode mentioned Early Error rules is not applied to the function declaration or function expression and JS just creates a valid function which you may call later without any errors:
在non-strict mode提到的Early Error规则不适用于函数声明或函数表达式,而JS只是创建一个有效函数,您以后可以调用它而不会出现任何错误:
function f(a,a){ console.log(a)}f(0,100)// 100 in consoleJS freedom is one love!
JS自由是一种爱!
Let’s check the arrow function parameters syntax in the spec:
让我们检查规范中的箭头函数参数语法:
ArrowFormalParameters :( StrictFormalParameters )
ArrowFormalParameters :( StrictFormalParameters )
It means that duplicate parameters Early Error rule is always applied to arrow function definition even if 'strict mode' is not defined explicitly.
这意味着即使未明确定义'strict mode' ,重复参数“早期错误”规则也始终应用于箭头功能定义。
Function declaration and function expression with duplicate parameters in 'strict mode' throw Syntax Error;
在'strict mode'具有重复参数的函数声明和函数表达式抛出语法错误;
Function declaration and function expression with duplicate parameters in 'non-strict mode' create a valid function;
在'non-strict mode'具有重复参数的函数声明和函数表达式创建有效的函数;
Arrow function definition with duplicate parameters always throws Syntax Error; 具有重复参数的箭头函数定义始终会引发语法错误; Keep calm and read specs :) 📕📗📘 保持冷静并阅读规格:)📕📗📘The ES6 spec contains Annex C — list of the strict mode restriction and exceptions. There is a point about our topic in that list as well.
ES6规范包含附件C-严格模式限制和例外列表。 该列表中也有关于我们主题的观点。
翻译自: https://medium.com/@andr.smolko/function-with-duplicate-parameters-your-turn-js-ffd42944dfe4
js post 重复参数
相关资源:微信小程序源码-合集6.rar