var let可以,但是let可以吗? [英] var let is ok but let let isn't?

查看:59
本文介绍了var let可以,但是let可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//Version A:
var let = true;
console.log(let);//true

//Version B:
let let = 0; //syntax Error: let is disallowed as a lexically bound name
console.log(let);

是否有特定的原因允许我们使用var创建的名称为let的变量,但不允许使用let?通过此实验,是否还意味着变量名称let可以存在于全局级别,但不能存在于某些范围级别?但是不是全球性的考虑范围吗?

Is there any particular reason where we allow variable with name let created using var but not let? With this experiment, is that also means that variable name let is ok to exist on global level but not in some scope level? But isn't global is consider scope?

推荐答案

因为规范如此.

来自

13.3.1允许和禁止声明

13.3.1.1静态语义:早期错误

LexicalDeclaration : LetOrConst BindingList ;

13.3.1 Let and Const Declarations

13.3.1.1 Static Semantics: Early Errors

LexicalDeclaration : LetOrConst BindingList ;

  • 如果 BindingList 的绑定名称包含 "let" ,则是语法错误.
  • It is a Syntax Error if the BoundNames of BindingList contains "let".

变量声明规范没有这样的限制,可能是因为在定义var时未使用let,并且更改var的规范将是一个重大更改.

The variable statement specification has no such limitation, probably because let was not in use at the time var was defined, and changing the specification for var would be a breaking change.

这篇关于var let可以,但是let可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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