为什么允许"let"作为变量名? [英] Why is 'let' allowed as a variable name?

查看:76
本文介绍了为什么允许"let"作为变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,是否有人可以将我发现的一些怪异行为引起关注.因此,我在节点REPL中工作,在将代码粘贴到模块中之前运行代码,然后打了一个'let = 5'的错字(嗯,没有完全填满这一行).我期望这会出错,但是REPL接受了它,并且可以在表达式结果和console.logs中看到该值.所以我开始修补.我知道在REPL中,没有let/const/var的变量被认为是全局变量,但是我想知道的是,为什么REPL允许我们这样分配let?下面,我列出了我尝试过的方法(仅在REPL中,未在模块脚本中尝试过).

  let = 5;//作品var = 5;//SyntaxError:意外的令牌"="const = 5;//SyntaxError:意外的令牌"="让让= 5;//SyntaxError:不允许以词汇绑定名称var let = 5;//作品const let = 5;//SyntaxError:不允许以词汇绑定名称令var = 5;//SyntaxError:意外的令牌"var";var var = 5;//SyntaxError:意外的令牌"var";const var = 5;//SyntaxError:意外的令牌"var";令const = 5;//SyntaxError:意外的令牌"const";var const = 5;//SyntaxError:意外的令牌"const";const const = 5;//SyntaxError:意外的令牌"const"; 

那么为什么 let = 5 var let = 5 在逻辑上(至少对我而言)起作用,那么所有这些语句应该都是语法错误?

编辑要添加的内容, let 在分配后用于分配变量时仍能发挥相同的作用.例如

  let = 5;让测试= {};console.log(let,test) 

有效,并显示 5 {}

解决方案

const let ECMA2011 .

ECMA2011-引入 const let 作为将来的保留字

7.6.1.2将来的保留字

以下字词在建议的扩展程序中用作关键字,并且因此保留以允许将来采用这些扩展中的一个.

FutureReservedWord ::

  • 课程
  • 枚举
  • 扩展
  • 超级
  • 常量
  • 导出
  • 导入

以下标记也被视为 FutureReservedWords 当它们出现在严格模式代码中时(请参见10.1.1).发生在任何情况下,在严格模式代码中的任何这些标记中, FutureReservedWord 的出现将产生错误,必须也会产生一个等效的错误:

  • 实现
  • 私有
  • 公开
  • 收益
  • 界面
  • 软件包
  • 受保护的
  • 静态

ECMA2012-批准的关键字

后面的 ECMA2012 两个单词都被添加为关键字,不能用作标识符.

7.6.1.1关键字

以下标记是ECMAScript关键字,不能用作ECMAScript程序中的标识符.

关键字::

  • 休息
  • 删除
  • 导入
  • 案例
  • 执行
  • 投掷
  • 抓住
  • 其他
  • 实例
  • 尝试
  • 课程
  • 导出
  • 类型
  • 继续
  • 最终
  • var
  • 常量
  • 用于
  • 返回
  • 无效
  • 调试器
  • 功能
  • 超级
  • 同时
  • 默认
  • 如果
  • 开关
  • 使用

用作 identifier const 会在每种模式下引发错误,而 let 仅在处于严格模式下会引发错误,而这种情况仍然存在如今使用您的示例进行案例分析:

有效

使用 let 作为没有严格模式的标识符.

 (function(){//REM:有效var let = 5;console.log(let);})();  

无效

使用 const 作为没有严格模式的标识符.

 (function(){//REM:引发错误var const = 5;console.log(const);})();  

在严格模式下使用 let const 作为标识符.

 (function(){使用严格";//REM:引发错误var let = 5;console.log(let);})();  

 (function(){使用严格";//REM:引发错误var const = 5;console.log(const);})();  

因此,从一开始,ECMA在使用关键字 const 时比使用 let 严格.尽管自ECMA2012起, let 可能不再用作 Identifier ,但我认为由于向后兼容,它被忽略了.

此处 let的最新规范 const .

I was wondering if someone could point the spotlight on some weird behavior I just found. So, I was working in the node REPL, running code before I stick it in a module, and I made a typo (well, didn't completely fill the line in) of 'let = 5'. I expected this to error out, but the REPL accepted it, and I can see the value with expression results and console.logs. So I started tinkering. I know that in the REPL, variables without let/const/var are considered global, but what I was wondering is, why does the REPL allow us to assign let like this? Below I have listed the things I've tried (in REPL only, have not tried in module script).

let = 5; //works
var = 5; //SyntaxError: Unexpected token "="
const = 5; //SyntaxError: Unexpected token "="

let let = 5; //SyntaxError: let is disallowed as a lexically bound name
var let = 5; //works
const let = 5; //SyntaxError: let is disallowed as a lexically bound name

let var = 5; //SyntaxError: Unexpected token "var"
var var = 5; //SyntaxError: Unexpected token "var"
const var = 5; //SyntaxError: Unexpected token "var"

let const = 5; //SyntaxError: Unexpected token "const"
var const = 5; //SyntaxError: Unexpected token "const" 
const const = 5; //SyntaxError: Unexpected token "const"  

So why does let = 5 and var let = 5 work when logically (to me at least), all of those statements should have been syntax errors?

Edit To add, let still works the same when used to assign a variable after being assigned. E.g.

let = 5; 
let test = {}; 
console.log(let,test)

works, and displays 5 {}

解决方案

const and let have been introduced as Future Reserved Words in ECMA2011.

ECMA2011 - Introduction of const and let as Future Reserved Words

7.6.1.2 Future Reserved Words

The following words are used as keywords in proposed extensions and are therefore reserved to allow for the possibility of future adoption of those extensions.

FutureReservedWord ::

  • class
  • enum
  • extends
  • super
  • const
  • export
  • import

The following tokens are also considered to be FutureReservedWords when they occur within strict mode code (see 10.1.1). The occurrence of any of these tokens within strict mode code in any context where the occurrence of a FutureReservedWord would produce an error must also produce an equivalent error:

  • implements
  • let
  • private
  • public
  • yield
  • interface
  • package
  • protected
  • static

ECMA2012 - Approved keywords

Later on in ECMA2012 both words were added as keywords, which may not be used as identifiers.

7.6.1.1 Keywords

The following tokens are ECMAScript keywords and may not be used as Identifiers in ECMAScript programs.

Keyword ::

  • break
  • delete
  • import
  • this
  • case
  • do
  • in
  • throw
  • catch
  • else
  • instanceof
  • try
  • class
  • export
  • let
  • typeof
  • continue
  • finally
  • new
  • var
  • const
  • for
  • return
  • void
  • debugger
  • function
  • super
  • while
  • default
  • if
  • switch
  • with

Whereas const used as identifier is throwing errors in every mode, let only throws an error while being in strict mode, which is still the case nowadays using your example:

Valid

Using let as identifier without strict mode.

(function(){
    //REM: Works
    var let = 5;
    console.log(let);
  })();

Invalid

Using const as identifier without strict mode.

(function(){
    //REM: Throws an error
    var const = 5;
    console.log(const);
})();

Using either let or const as identifiers in strict mode.

(function(){
    'use strict';
    //REM: Throws an error
    var let = 5;
    console.log(let);
})();

(function(){
    'use strict';
    //REM: Throws an error
    var const = 5;
    console.log(const);
})();

So historically ECMA was more strict with the keyword const than let from the get go. While let may not be used as Identifier since ECMA2012, I assume it was overlooked due to backwards compatibility.

Here is the latest specification of let and const.

这篇关于为什么允许"let"作为变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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