使用'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.
方案:
Scenarios:
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
这个缺陷存在于节点
。但是,当我在 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之前的代码不会中断 - 如果某人已经命名了很多变量让在传统应用程序中,如果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屋!