不能使用let来定义与函数的参数名称相同的变量 [英] Cannot use let to define a variable with the same name of function's parameter

查看:48
本文介绍了不能使用let来定义与函数的参数名称相同的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间后,我将返回javaScript,并且我刚刚了解了ES6的新功能.这是第一个疑问.

I am returning to javaScript after a long absence and I've just learnt about ES6 new features. And here it comes the first doubt.

尽管我知道在相同的块作用域中不能两次声明 let 变量,但我遇到过这种情况:

Although I understand that a let variable cannot be declared twice in the same block scope I came across this situation:

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

这给了我一个错误:未捕获的SyntaxError:标识符'story'已被声明

使用 var 可以正常工作.

1)这是否意味着我必须使用 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)? 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)在这种情况下,变量 story 是否将完全属于函数 tell 的范围?

2) In this case the variable story will belong exclusively to the scope of the function tell?

推荐答案

使用 var 可以正常工作.

不是真的, var 被完全忽略了,您实际上将拥有:

Not really, the var is completely ignored, and you'd effectively have:

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

如果这就是您想要的,只需完全关闭 var (和 let )(如上所述).

If that's what you want, just leave the var (and let) off entirely (as above).

let 中的错误解决了这个陷阱,您认为您已经在其中创建了(新)变量,但还没有.您不能使用 let (或 const )来声明已在相同范围内声明的变量(作为变量或参数,几乎是与变量相同).

The error from let addresses this pitfall, where you think you've created a (new) variable but haven't. 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).

1)这是否意味着我必须使用 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)? This can be very annoying in terms of variable naming.

由您决定.为了继续做您正在做的事情,只需完全放弃 var let const ,因为同样, var 完全无效,您正在分配给参数,而不是新变量.或继续做您正在做的事情(使用 var ),但要知道这是一种误导.或在 let const –中使用其他名称.许多人认为更改参数的值不是最佳实践(个人而言,我不是其中之一,但是...),他们建议使用其他变量.

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. Or keep doing what you were doing (with var), but understand that it's misleading. 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.

2)在这种情况下,变量 story 将完​​全属于函数 tell 的范围?

2) In this case the variable story will belong exclusively to the scope of the function tell?

是的,您要分配给 tell 本地的参数 story .

Yes, you're assigning to the parameter story, which is local to tell.

这篇关于不能使用let来定义与函数的参数名称相同的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆