[英]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 evenconst
)?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.要继续做你正在做的事情,只需完全放弃
var
、 let
和const
,因为再一次, 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.或者使用
let
或const
使用不同的名称——许多人认为更改参数的值不是最佳实践(我个人不是其中之一,但是...),他们会推荐不同的变量.
2) In this case the variable
story
will belong exclusively to the scope of the functiontell
?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.