Node.js 中实现的ES6 新特性

node.png


文章目录


ES6 的发布,给 JavaScript 开发带来了很多实实在在的特性,那么对于 node.js 的开发者而言,V8对于ES6新特性的支持有哪些?


如何查看node 的新特性

process.versions
➜  /Users/zhangzhi  >node
 process.versions
{ http_parser: '2.5.0',
  node: '4.2.4',
  v8: '4.5.103.35',
  uv: '1.7.5',
  zlib: '1.2.8',
  ares: '1.10.1-DEV',
  icu: '56.1',
  modules: '46',
  openssl: '1.0.2e' }

我当前的 V8 版本是 4.5.103.35

根据此版本号,查看V8更新日志中该版本支持了哪些新特性.


目前node.js支持的ES6新特性

  • 快级作用域
  • typeof null 等于 "null"
  • let 和 const
  • Map 和 WeakMap
  • 模块
  • 代理

Harmony 选项

当你运行命令 node --v8-options 可以显示出所有V8相关的选项.


➜  /Users/zhangzhi  >node --v8-options
SSE3=1 SSE4_1=1 SAHF=1 AVX=1 FMA3=1 BMI1=1 BMI2=1 LZCNT=1 POPCNT=1 ATOM=0
Usage:
  shell [options] -e string
    execute string in V8
  shell [options] file1 file2 ... filek
    run JavaScript scripts in file1, file2, ..., filek
  shell [options]
  shell [options] --shell [file1 file2 ... filek]
    run an interactive JavaScript shell
  d8 [options] file1 file2 ... filek
  d8 [options]
  d8 [options] --shell [file1 file2 ... filek]
    run the new debugging shell

Options:
  --use_strict (enforce strict mode)
        type: bool  default: false
  --use_strong (enforce strong mode)
        type: bool  default: false
  --strong_mode (experimental strong language mode)
        type: bool  default: false
  --strong_this (don't allow 'this' to escape from constructors)
        type: bool  default: true
  --es_staging (enable all completed harmony features)
        type: bool  default: false
  --harmony (enable all completed harmony features)
        type: bool  default: false
  --harmony_shipping (enable all shipped harmony fetaures)
        type: bool  default: true
  --legacy_const (legacy semantics for const in sloppy mode)
        type: bool  default: true
  --harmony_modules (enable "harmony modules" (in progress))
        type: bool  default: false
        

V8 相关选项太多,不一一列举:

  • --harmony_scoping: 激活harmony中的块级作用域
  • --harmony_modules: 激活harmony中的模块
  • --harmony_proxies: 激活harmony中的代理
  • --harmony_collections: 激活harmony中的集合类型(Set,Map,WeakMap)
  • --harmony_typeof: 激活harmony中typeof的新特性(typeof null等于"null")
  • --harmony 激活上面所有的 harmony 特性 (除了 typeof 特性以外)

如果你想在你的 node 版本下使用 ES6新特性

node --harmony app.js

出自:Node.js 中实现的ES6 新特性

回到顶部