为什么var允许重复声明,但为什么const和不允许重复声明? [英] Why does var allow duplicate declaration but why does const and let not allow duplicate declaration?

查看:597
本文介绍了为什么var允许重复声明,但为什么const和不允许重复声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var 是允许重复声明

xx=1;
xx=2;
console.log(xx+xx);//4

var xx=1;
var xx=2;
console.log(xx+xx);//4

 

const 不允许重复减速

const yy=1;
const yy=2;
console.log(yy+yy);//Uncaught SyntaxError: Identifier 'yy' has already been declared",

let zz=1;
let zz=2;
console.log(zz+zz);//Uncaught SyntaxError: Identifier 'zz' has already been declared",

我在 here


假设严格模式, var 将允许您在同一范围内重新声明相同的变量。另一方面,不会。

Assuming strict mode, var will let you re-declare the same variable in the same scope. On the other hand, let will not.

但我想知道为什么 const 不允许重新声明?为什么 var 呢?以及JavaScript如何处理这三种类型的减速?

But I want to know Why let and const doesn't allow re-declaration? and why var does? and How JavaScript handle these three type of deceleration ?

推荐答案

var



var 关键字是在2016 *之前定义变量的唯一方法。

var

The var keyword was the only way to define variables until 2016*.

无论你在哪里写 var x ,变量 x 被视为在封闭范围的顶部声明它(<$ c的范围) $ c> var 是函数)。

No matter where you write var x, the variable x is treated as if it were declared at the top of the enclosing scope (scope for var is "a function").

同一范围内变量的所有声明有效地谈论同一个变量。

All declarations of the variable within the same scope are effectively talking about the same variable.

这是一个例子......你可能认为在函数中我们覆盖了外部的名称使用 fenton ,并将 Fenton 添加到内部变量...

Here is an example... you might think that within the function we overwrite the outer name with fenton, and add Fenton to the inner variable...

var name = 'Ramesh';

function myFunc() {
    name = 'fenton';

    var name = 'Fenton';

    alert(name);

}

myFunc();

alert(name);

实际上,它就像这样......外部变量不受内部变量的影响感谢吊装。

In fact, it works just like this... the outer variable is not affected by the inner variable thanks to hoisting.

var name = 'Ramesh';

function myFunc() {
    var name;

    name = 'fenton';

    name = 'Fenton';

    alert(name);

}

myFunc();

alert(name);




  • 实际上,您也可以通过不使用<$隐式声明它们c $ c> var 关键字,在这种情况下,它们将被添加到全局范围。经常跟踪微妙的错误。

    • Actually, you could also declare them implicitly by not using the var keyword at all, in which case they would be added to the global scope. Subtle bugs were often tracked to this.
    • 两者 const 是块范围的,而不是函数范围的。这使得它们在大多数其他类C语言中像变量一样工作。事实证明,这比功能范围的变量更容易混淆。

      Both let and const are block-scoped, not function-scoped. This makes them work like variables in most other C-like languages. It turns out this is just less confusing than function-scoped variables.

      他们也更加自律。它们应该在块中声明一次。

      They are also both "more disciplined". They should be declared just once within a block.

      const 关键字也不允许后续分配 - 所以你必须用赋值声明它(即你不能只写 const x ,你必须写 const x ='Fenton') - 你以后不能再分配另一个值。

      The const keyword also disallows subsequent assignments - so you have to declare it with an assignment (i.e. you can't just write const x, you have to write const x = 'Fenton') - and you can't assign another value later.

      有些人认为这会使值不可变,但这是一个错误,因为值可以变异,如如下所示:

      Some people think this makes the value immutable, but this is a mistake as the value can mutate, as shown below:

      const x = [];
      
      // I can mutate even though I can't re-assign
      x.push('Fenton');
      
      // x is now ['Fenton']
      



      为什么会这样问题?



      如果你想避免一些比较混乱的 var 方面,比如多个声明都有所贡献对于相同的提升变量和函数范围,您应该使用较新的 const let 关键字。

      Why Does it Matter?

      If you want to avoid some of the more confusing aspects of var, such as multiple declarations all contributing to the same hoisted variable, and function-scope, you should use the newer const and let keywords.

      我建议使用 const 作为默认关键字,并将其升级为 let 仅在您选择允许重新分配的情况下。

      I recommend using const as your default keyword, and upgrade it to let only in cases where you choose to allow re-assignment.

      这篇关于为什么var允许重复声明,但为什么const和不允许重复声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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