使用“let"作为变量名不会在 google v8 中引发任何错误 [英] Using 'let' as a variable name is not throwing any errors in google v8
问题描述
我在 chrome 开发者控制台中编写了一些随机代码.令我惊讶的是,chrome 让我使用 let
作为变量名,这是完全错误的,因为 let
是保留关键字.我需要了解为什么会发生这种情况.
I was writing some random code in the chrome developer console. For my surprise, chrome let me use let
as a variable name which is completely wrong as let
is a reserved keyword. I need to understand why is this happening.
场景:
var const = 78 //throws an error as expected
var function = 46 //throws an error as expected
var let = 56 //didn't throw an error :O
let //prints 56, which is wrong because 'let' is a keyword
let ab = 90
ab //prints 90 as expected
这个缺陷存在于node
.但是,当我在 Babel REPL 抛出错误.
This flaw exists in node
. But, when I try it in Babel REPL it is throwing an error.
我认为这与 Google v8 有关
I think this is something to do with Google v8
推荐答案
可以在 这篇文章来自 Mohsen Azimi.这是它的快速摘要.
A nice write-up of the reasoning behind this can be found in this article by Mohsen Azimi. Here's a quick summary of it.
以下关键字在 JavaScript 规范中定义为 FutureReservedWord
:
The following keywords are defined in the JavaScript spec as FutureReservedWord
:
implements interface let package private
protected public static yield
在普通模式下,这些可以用作变量名而不会出错;但是,在严格模式下,它们被视为保留字,并会抛出以下错误:
In normal mode, these can be used as variable names without errors; however, in strict mode they are treated as reserved words and will throw the following error:
SyntaxError: Cannot use the reserved word 'let' as a variable name in strict mode.
这样做是为了使 ES2015 之前的代码不会被破坏——如果有人在遗留应用程序中命名了很多变量 let
,如果 JS 规范突然被破坏,他们可能会不高兴一切.
This is so that pre-ES2015 code doesn't break - if someone had named lots of their variables let
in a legacy app, they probably wouldn't be happy if the JS spec suddenly broke everything.
这篇关于使用“let"作为变量名不会在 google v8 中引发任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!