javascript- Uncaught SyntaxError: Identifier * has already been declared

前端 未结 3 1553
盖世英雄少女心
盖世英雄少女心 2020-12-24 07:52
console.log(a) //output:ƒ a(){}
var a = 1;
function a(){};
var a = 10;
console.log(a) //output:10

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

var a = 1;
i         


        
3条回答
  •  独厮守ぢ
    2020-12-24 08:16

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

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

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

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

提交回复
热议问题