为什么要为变量“let”指定值?可能? [英] Why is assigning a value to the variable "let" possible?

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

问题描述

我正在参加在线JS课程,讲师使用语法: let = names = [Bob,Tim,Larry] 。我确信这是一次意外,但他正在使用的JS环境允许。他最终做的事情(可能是偶然的)是将数组分配给names变量,该变量将其分配给let变量。

I am taking an online JS course and the instructor used the syntax : let = names = ["Bob","Tim","Larry"]. I am convinced that was an accident, but somehow allowed by the JS environment he was using. What he ended up doing (probably by accident) was assign the array to the "names" variable, which assigned it to the "let" variable.

我的问题:为什么这不是错误?为什么可以让用作变量?我们知道let是一个关键字。或者它只是他们尚未取缔的东西?您仍然可以在Chrome和Firefox控制台中执行此操作,也可以在Node中执行此操作。

My question: why is this not an error? Why can "let" be used as an variable? We know "let" is a keyword. Or is it just something they haven't outlawed yet? You can still do it in the Chrome and Firefox console... and in Node, too, for that matter.

推荐答案

在ES3及早期的JavaScript版本中,没有任何特殊含义,可用作标识符。但是, const 已经在未来保留字的列表中。参见 http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf 7.5.1
保留字

In ES3 and earlier JavaScript versions, let didn't have any special meaning and could be used as an identifier. const, however, was already on the list of future reserved words. See http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf, 7.5.1 Reserved Words.

ES5添加了严格模式。 const 仍在未来保留字的列表中,而在严格模式下被视为未来的保留字: http://www.ecma-international .org / ecma-262 / 5.1 / index.html#sec-7.6.1

ES5 added "strict mode". const is still on the list of future reserved words and let is to be treated like a future reserved word in strict mode: http://www.ecma-international.org/ecma-262/5.1/index.html#sec-7.6.1

ES6赋予 const <的含义/ code>和,但仍然不是保留字。相反它在 http://www.ecma-international.org中说/ecma-262/6.0/#sec-keywords

ES6 gave a meaning to const and let, but let is still not a reserved word. Instead it says in http://www.ecma-international.org/ecma-262/6.0/#sec-keywords:


严格模式代码 static 通过静态语义限制被视为保留关键字(参见 12.1.1 13.3.1.1 13.7.5.1 14.5.1 )而不是词汇语法。

In strict mode code, let and static are treated as reserved keywords through static semantic restrictions (see 12.1.1, 13.3.1.1, 13.7.5.1, and 14.5.1) rather than the lexical grammar.

换句话说,在严格模式之外,您可以使用 let 作为变量名称并声明其他变量。

In other words, outside of strict mode, you can use let both as a variable name and to declare other variables.

未成为保留字的原因可能是不破坏可能使用 let 作为普通标识符。

The reason let wasn't made into a reserved word is probably to not break existing code that may have used let as a normal identifier.

这篇关于为什么要为变量“let”指定值?可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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