简体   繁体   English

不能使用 let 定义与函数参数同名的变量

[英]Cannot use let to define a variable with the same name of function's parameter

I am returning to javaScript after a long absence and I've just learnt about ES6 new features.久违了,我又回到了 javaScript,我刚刚了解了 ES6 的新特性。 And here it comes the first doubt.这是第一个疑问。

Although I understand that a let variable cannot be declared twice in the same block scope I came across this situation:虽然我知道let变量不能在同一个块范围内声明两次,但我遇到了这种情况:

    function tell(story){
       let story = {
          story
        };
     }

And it gives me a error: Uncaught SyntaxError: Identifier 'story' has already been declared它给了我一个错误: Uncaught SyntaxError: Identifier 'story' has already been declared

Using var this works just fine.使用var这工作得很好。

1) Does this mean that I will have to declare a variable with a different name of the function's parameter using let (or even const )? 1)这是否意味着我必须使用let (甚至const )声明一个具有不同名称的函数参数的变量? This can be very annoying in terms of variable naming.这在变量命名方面可能非常烦人。

In order to keep the same name I did:为了保持相同的名称,我做了:

function tell(story){
     story = {
        story
     };
}

2) In this case the variable story will belong exclusively to the scope of the function tell ? 2) 在这种情况下,变量story将完全属于函数tell的范围?

Using var this works just fine.使用var这工作得很好。

Not really, the var is completely ignored, and you'd effectively have:不是真的, var完全被忽略了,你实际上有:

function tell(story){
   story = {
      story
   };
 }

If that's what you want, just leave the var (and let ) off entirely (as above).如果这是您想要的,只需完全关闭var (和let )(如上)。

The error from let addresses this pitfall, where you think you've created a (new) variable but haven't. let的错误解决了这个陷阱,您认为您已经创建了一个(新)变量但没有。 You can't use let (or const ) to declare a variable that's already declared in the same scope (either as a variable, or as a parameter, which is almost the same thing as a variable).您不能使用let (或const )来声明已在同一范围内声明的变量(作为变量或作为参数,这与变量几乎相同)。

1) Does this mean that I will have to declare a variable with a different name of the function's parameter using let (or even const )? 1)这是否意味着我必须使用let (甚至const )声明一个具有不同名称的函数参数的变量? This can be very annoying in terms of variable naming.这在变量命名方面可能非常烦人。

That's up to you.这取决于你。 To keep doing what you were doing, just leave off var , let , and const entirely, because again, the var had no effect at all and you were assigning to the parameter, not a new variable.要继续做你正在做的事情,只需完全放弃varletconst ,因为再一次, var根本没有任何效果,你正在分配给参数,而不是新变量。 Or keep doing what you were doing (with var ), but understand that it's misleading.或者继续做你正在做的事情(使用var ),但要明白这是误导。 Or use a different name with let or const — there are many who believe changing the value of a parameter isn't best practice (personally I'm not one of them, but...) and they'd recommend a different variable instead.或者使用letconst使用不同的名称——许多人认为更改参数的值不是最佳实践(我个人不是其中之一,但是...),他们会推荐不同的变量.

2) In this case the variable story will belong exclusively to the scope of the function tell ? 2) 在这种情况下,变量story将完全属于函数tell的范围?

Yes, you're assigning to the parameter story , which is local to tell .是的,您正在分配参数story ,这是 local to tell

"the var is completely ignored" “var 被完全忽略”

Not really, consider the following two examples:并非如此,请考虑以下两个示例:

1: 1:

function someFn(a, b = () => a) {
  console.log(a, b());

  a = 2;
  
  console.log(a, b());
}
someFn(3);

2: 2:

function someFn(a, b = () => a) {
  console.log(a, b());

  var a = 2;
  
  console.log(a, b());
}
someFn(3);

In the first example the output will be:在第一个示例中,输出将是:
3 3
2 2
And in the second one:在第二个中:
3 3
2 3

So var does matter.所以var确实很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能用 let 声明一个和 function 参数同名的变量,连参数都设置默认值? - Why can't use let to declare a variable using the same name as one function parameter, even the parameter get set default value? JavaScript:使用函数参数定义变量名称 - JavaScript: Define variable name with function parameter 如何使用函数参数来引用同名变量? - How to use a function parameter to refer to the variable of the same name? Babel重新声明函数参数如果使用相同名称的'let'两次(在for循环中) - Babel redeclares function parameter if 'let' with the same name is used twice (in a for loop) 将变量分配为具有相同名称的 function 参数 - Assign a variable as a function parameter with the same name Javascript闭包(使用参数名称在同一函数中定义对象) - Javascript closure (using parameter name to define an object in the same function) 使用传递给函数的参数作为变量名 - Use a parameter passed to a function as a variable name 无法将状态变量用作功能参数? 反应 - Cannot use the state variable as function parameter? React 从参数中使用相同名称的 function 访问 ES 模块变量 - Access a ES module variable from a function which use the same name in parameter JavaScript:与其他变量同名的回调函数参数? - JavaScript: callback function parameter with same name as other variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM