string.includes('a')函数未在Node.js 0.12中定义?

Kil*_*den 2 node.js ecmascript-6

我理解include()数组函数仅支持ECMAscript 6+,但现在Node.js中是否已支持ECMAscript 6?我也尝试过:

node --harmony app.js
Run Code Online (Sandbox Code Playgroud)

但那给了我一个:

node: bad option: --harmony
Run Code Online (Sandbox Code Playgroud)

在我的Windows 8机器上.

我也尝试过使用string.contains('a'); 但是那个数组函数也没有定义.

Giu*_*Pes 5

String.prototype.includes在Node中可用String.prototype.contains.看看这个:http: //kangax.github.io/compat-table/es6/.看看标志26.我认为,最初标准中提出的名称是,contains但由于兼容性问题,它已被更改.

V8 Pull请求:https://codereview.chromium.org/742963002

您可以使用以下选项查看节点版本支持的选项node --v8-options | grep harm.确保您使用的是节点的更新版本.

这是节点版本0.12.2的输出

Giuseppes-Air:development giuseppe$ node --v8-options | grep harm
  --harmony_scoping (enable harmony block scoping)
  --harmony_modules (enable harmony modules (implies block scoping))
  --harmony_proxies (enable harmony proxies)
  --harmony_generators (enable harmony generators)
  --harmony_numeric_literals (enable harmony numeric literals (0o77, 0b11))
  --harmony_strings (enable harmony string)
  --harmony_arrays (enable harmony arrays)
  --harmony_arrow_functions (enable harmony arrow functions)
  --harmony (enable all harmony features (except proxies))
Run Code Online (Sandbox Code Playgroud)

从输出中可以看出,harmony使用选项时会启用箭头字符串(=>),字符串和数组.

如果你想充分享受ES6,我建议使用babelbabel-node.String.prototype.includes正确支持.