javascript-未捕获的语法错误:标识符 * 已被声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49774769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:28:56  来源:igfitidea点击:

javascript- Uncaught SyntaxError: Identifier * has already been declared

javascriptscopevarhoisting

提问by venkata

console.log(a) //output:? a(){}
var a = 1;
function a(){};
var a = 10;
console.log(a) //output:10

====================

====================

var a = 1;
if(true){
  function a(){};
  var a = 10;
}
console.log(a) // this code throws Uncaught SyntaxError: Identifier 'a' has already been declared

both above code snippets are same except the if block.why does the latter throws error when its permissible in javascript to delcare same variable twice in the same scope with var as below

除了 if 块之外,上面的两个代码片段都是相同的。 为什么后者在 javascript 中允许在同一范围内使用 var 在同一范围内两次 delcare 相同变量时抛出错误,如下所示

 function a(){};
 var a = 10; //no error

Also for a slightly different scenario after removing var from `var a = 10 in the above code ,then it works fine but output is surprising

同样对于在上面的代码中从 `var a = 10 中删除 var 之后的稍微不同的情况,然后它工作正常但输出令人惊讶

 var a = 1;
 if(true) {
   function a(){};
   a = 10;
 }
 console.log(a) //output:? a(){}

I am surprised to see this output as I am expecting 10 ..because two variables declared inside the if block refer to the same variable declared above as javascript var doesnt respect block scope but functional scope...so why not the output for above should be 10? where as the below code outputs 10 as i expected when replaced the function definition with function expression.

我很惊讶地看到这个输出,因为我期待 10 ..因为在 if 块中声明的两个变量引用了上面声明的相同变量,因为 javascript var 不尊重块范围而是功能范围......所以为什么不上面的输出应该是 10?当用函数表达式替换函数定义时,下面的代码输出 10 正如我预期的那样。

  var a = 1;
  if(true) {
    var a = function(){ console.log() }
    a = 10;
  }
  console.log(a) //output:10

采纳答案by Bergi

This is surprising as javascript vardoesn't respect block scope but functional scope...

这是令人惊讶的,因为 javascriptvar不尊重块范围而是功能范围......

Sure, but you didn't use varfor the declaration of ain the block scope. You used a function declaration, which doesrespect block scopes(otherwise it would be completely invalid code, as in ES5 strict mode).

当然,但是您没有在块作用域中使用varfor 的声明a。您使用了一个函数声明,它确实尊重块作用域(否则它将是完全无效的代码,就像在 ES5 严格模式中一样)。

It's permissible in javascript to declare same variable twice in the same scope with varas below

在 javascript 中允许在相同的范围内声明相同的变量两次,var如下所示

Same applies here. The functiondeclaration in the block uses ES6 declaration semantics (like letor const), which does not allow redeclarations.

这里同样适用。function块中的声明使用 ES6 声明语义(如letconst),不允许重新声明。

回答by Nikhil Aggarwal

Case 1

情况1

console.log(a) //output:? a(){}
var a = 1;
function a(){};
var a = 10;
console.log(a) //output:10

Will be rendered as

将呈现为

var a;
a = function(){}; // now a holds the value as a function
console.log(a); // output : f a(){}
a = 1; // a is a var that holds value 1
a = 10; // a is a var that holds value 10
console.log(a); // output : 10

Case 2

案例二

var a = 1;
if(true){
   function a(){};
   var a = 10;
}
console.log(a)

Will be rendered as

将呈现为

var a;
a = 1;
if(true) {
    a = function() {};
    let a; // The function declaration in the block uses ES6 declaration semantics (like let or const), which does not allow re-declarations.
    var a; // throws Uncaught SyntaxError: Identifier 'a' has already been declared
    a = 10;
}
console.log(a);

Case 3

案例3

var a = 1;
if(true){
    function a(){};
    a = 10;
 }
console.log(a)

Will be rendered as

将呈现为

var a;
a = 1;
if(true) {
    a = function() {};
    let a;
    a = 10;
}
console.log(a); // output : f a(){}

Case 4

案例四

var a = 1;
if(true){
    var a= function(){console.log()}
    a = 10;
}
console.log(a)

Will be rendered as

将呈现为

var a;
a = 1;
if(true) {
    a = function(){console.log()}
    a = 10;
}
console.log(a) // output:10

Case 5

案例5

var a = 1;
if(true){
    function a(){};
    a = 10;
    console.log(a) 
}
console.log(a) 

Will be rendered as

将呈现为

var a;
a = 1;
if(true){
    a = function() {};
    let a;
    a = 10;
    console.log(a); // output:10
}
console.log(a); // output : f a(){}

回答by Mohd Sahil

The simple solution to this is to use IIFE

对此的简单解决方案是使用 IIFE

(function() {
var sahil = {
  checkThis: function() {
    console.log(this);

    function checkOther() {
      console.log(this);
    }
    checkOther(); // checkThis() function called in "global context", will
                  // return "this" as "window"
  }
};
var moo = sahil.checkThis;
moo(); // moo() function called in "global context", will return "this" as "window" })();