我如何使用ES6语法,如在chrome控制台中? [英] How can i use ES6 syntax such as let in chrome console?
问题描述
我正在Chrome控制台中编写一些 ES6 代码,但我遇到一些错误。如何在控制台中运行 ES6 脚本?
I am trying to write some ES6 code in chrome console but i ran with some errors. How can i run the ES6 scripts in console?
例如,给定输入
let type='grizzle';
控制台用消息记录一个 SyntaxError
the console records a SyntaxError
with the message
在严格模式外不支持块范围声明(let,const,function,class)
Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
,如下面的截图所示:
推荐答案
正如错误信息所述,某些ES6功能在严格模式之外不可用。因此,要利用这些功能,您必须先创建一个严格模式块。
As the error message states, some ES6 features are not available outside of strict mode. Therefore, to take advantage of those features, you must first create a strict-mode block.
从控制台开始,使用严格模式的最简单的方法是通过创建一个即时调用的函数表达式(IIFE)。例如,
From the console, the easiest way to use strict mode is by creating an Immediately-Invoked Function Expression (IIFE). For example,
(function() { "use strict"; let x = "asdf"; }());
将在控制台中输入时的行为。
will behave as intended when entered in the console.
这篇关于我如何使用ES6语法,如在chrome控制台中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!