关于在nodejs上使用es6特性

首先,不管怎样,先把nodejs升级到v5.6。

然后执行 

node --v8-options | grep harmony



就可以看到当前node支持的新特性。

--es_staging (enable all completed harmony features)
  --harmony (enable all completed harmony features)
  --harmony_shipping (enable all shipped harmony fetaures)
  --harmony_modules (enable "harmony modules" (in progress))
  --harmony_regexps (enable "harmony regular expression extensions" (in progress))
  --harmony_proxies (enable "harmony proxies" (in progress))
  --harmony_sloppy_function (enable "harmony sloppy function block scoping" (in progress))
  --harmony_sloppy_let (enable "harmony let in sloppy mode" (in progress))
  --harmony_unicode_regexps (enable "harmony unicode regexps" (in progress))
  --harmony_reflect (enable "harmony Reflect API" (in progress))
  --harmony_destructuring (enable "harmony destructuring" (in progress))
  --harmony_default_parameters (enable "harmony default parameters" (in progress))
  --harmony_sharedarraybuffer (enable "harmony sharedarraybuffer" (in progress))
  --harmony_atomics (enable "harmony atomics" (in progress))
  --harmony_simd (enable "harmony simd" (in progress))
  --harmony_array_includes (enable "harmony Array.prototype.includes")
  --harmony_tostring (enable "harmony toString")
  --harmony_concat_spreadable (enable "harmony isConcatSpreadable")
  --harmony_rest_parameters (enable "harmony rest parameters")
  --harmony_sloppy (enable "harmony features in sloppy mode")
  --harmony_arrow_functions (enable "harmony arrow functions")
  --harmony_new_target (enable "harmony new.target")
  --harmony_object_observe (enable "harmony Object.observe")
  --harmony_spreadcalls (enable "harmony spread-calls")
  --harmony_spread_arrays (enable "harmony spread in array literals")
  --harmony_object (enable "harmony Object methods")



可以看到有一些特性是 (in progress) 状态,这些需要单独加参数,比如我想用 "destructuring 变量结构赋值",那必须这样

node --harmony_destructuring --use_strict



注意 --use_strict ,这个特性必须在 strict模式下才生效。

在脚本中需要加 "use strict" ,无需多说。



你可能感兴趣的:(JavaScript,nodejs)