SyntaxError:在严格模式下使用const [英] SyntaxError: Use of const in strict mode

查看:102
本文介绍了SyntaxError:在严格模式下使用const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js,在我的一个js文件中,我在严格模式中使用 const 。当我试图运行它时,我收到一个错误:

I'm working with node.js, and in one of my js files I'm using const in "strict mode". When trying to run it, I'm getting an error:

SyntaxError: Use of const in strict mode.

这样做的最佳做法是什么?

What is the best practice to do this?

编辑:

'use strict'
const MAX_IMAGE_SIZE = 1024*1024; // 1 MB


推荐答案

const let 是ECMAScript 2015(又名ES6和Harmony)的一部分,默认情况下在Node.js 0.10或0.12中未启用。从Node.js 4.x开始,所有出货[ES2015]功能,V8认为稳定,默认情况下在Node.js上打开,不需要任何类型的运行时标志。 Node.js文档概述了默认情况下启用的ES2015功能,以及需要运行时标志的人员。因此,通过升级到Node.js 4.x或更新版本,错误应该消失。

The const and let are part of ECMAScript 2015 (a.k.a. ES6 and Harmony), and was not enabled by default in Node.js 0.10 or 0.12. Since Node.js 4.x, "All shipping [ES2015] features, which V8 considers stable, are turned on by default on Node.js and do NOT require any kind of runtime flag.". Node.js docs has an overview of what ES2015 features are enabled by default, and which who require a runtime flag. So by upgrading to Node.js 4.x or newer the error should disappear.

启用一些ECMAScript 2015功能(包括 const < Node.js 0.10和0.12中的/ code>和 let );使用和声标志启动节点程序,否则会出现语法错误。例如:

To enable some of the ECMAScript 2015 features (including const and let) in Node.js 0.10 and 0.12; start your node program with a harmony flag, otherwise you will get a syntax error. For example:

node --harmony app.js

这完全取决于您的严格js所在的一侧。我建议在服务器端使用带有 const 声明的严格模式,并使用和声标志启动服务器。对于客户端,您应该使用 Babel 或类似工具将ES2015转换为ES5,因为并非所有客户端浏览器都支持 const 声明。

It all depends on which side your strict js is located. I would recommend using strict mode with const declarations on your server side and start the server with the harmony flag. For the client side, you should use Babel or similar tool to convert ES2015 to ES5, since not all client browsers support the const declarations.

这篇关于SyntaxError:在严格模式下使用const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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