同名的变量和函数在块内返回错误 [英] a variable and function with same name returns an error inside a block
问题描述
如果我们声明一个变量和一个同名的函数,就是接受重新声明.但是当我们在一个块内做同样的事情时,它会显示重新声明错误.
代码:
If we declare a variable and a function with same name, it is accepting re-declaration. But when we do the same thing inside a block, it shows re-declaration error.
Code:
var x;
function x() {}; // no error.
但在这种情况下,我收到错误.
But in this case i'm getting Error.
{
var inside; // re-declaration error.
function inside() {};
}
预期结果应该没有错误.
expected result should be no error.
推荐答案
这是 EcmaScript 6 的更改.从 ES6 开始,它不再允许在块范围内有重复的绑定.
This is an EcmaScript 6 change. From ES6 onwards it's no longer allowed to have duplicate bindings within a block scope.
ES5 规范没有这样的限制,但在 ES6 规范 中,语义已更改:
The ES5 spec does not have such a restriction but in the ES6 spec the semantics have been changed:
13.2.1 静态语义:早期错误
阻止 : { StatementList }
如果 StatementList 的 LexicallyDeclaredNames 包含任何重复条目,则会出现语法错误.
It is a Syntax Error if the LexicallyDeclaredNames of StatementList contains any duplicate entries.
如果StatementList的LexicallyDeclaredNames的任何元素也出现在StatementList的VarDeclaredNames中,则为语法错误.
It is a Syntax Error if any element of the LexicallyDeclaredNames of StatementList also occurs in the VarDeclaredNames of StatementList.
第一部分是相关的 - LexicallyDeclaredNames
包含在块内的代码中找到的所有声明.
The first part is relevant - LexicallyDeclaredNames
contains all declarations found within the code inside the block.
据推测,这是 ES6 中语义函数声明更改的一部分,因为现在它们可以是块作用域:
Presumably, this is part of a change in semantics function declarations in ES6, since now they can be block scoped:
{ //block 1
function foo() { // declared in block 1
return 1;
}
console.log("block 1: foo() === 1", foo() === 1);
{ // block 2
function foo() { // declared in block 2
return 2;
}
console.log("block 2: foo() === 2", foo() === 2);
}
console.log("block 1: foo() === 1", foo() === 1);
}
这是对等效 ES5 代码的一种语法糖:
This is a syntactic sugar over this equivalent ES5 code:
(function() { //block 1
var foo = function() {
return 1;
}
console.log("block 1: foo() === 1", foo() === 1);
(function() { //block 2
var foo = function() {
return 2;
}
console.log("block 2: foo() === 2", foo() === 2);
})();
console.log("block 1: foo() === 1", foo() === 1);
})();
但是,此功能不能用于重复名称.
However, this feature cannot work with duplicate names.
对于任何块,包括其他类型的块语句,相同的行为都会持续.下面是一个例子:
The same behaviour persists for any block, including other types of block statements. Here is an example:
{ //block
function foo() { return 1; }
console.log("block: foo() === 1", foo() === 1);
if (true) { // if block
function foo() { return 2; }
console.log("if block: foo() === 2", foo() === 2);
}
for (var i = 0; i < 1; i++) { // for block
function foo() { return 3; }
console.log("for block: foo() === 3", foo() === 3);
}
switch ("hello world") { // case block
default:
function foo() { return 4; }
console.log("case block: foo() === 4", foo() === 4);
}
console.log("block: foo() === 1", foo() === 1);
}
但是,需要注意的是,相同类型(var
或function
)的重复声明不会导致错误:
However, it should be noted that duplicate declaration of the same type (var
or function
) do not lead to an error:
{
var foo = 1;
var foo = 2;
console.log("foo ->", foo);
}
{
function bar() { return "a"; }
function bar() { return "b"; }
console.log("bar() ->", bar());
}
因此,它们似乎没有被视为不同声明,而是覆盖了相同的词法声明名称.
So, it seems like they aren't treated as different declarations but overwriting the same lexically declared name.
这篇关于同名的变量和函数在块内返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!