作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我确实了解 includes() 数组函数仅支持 ECMAscript 6+,但 ECMAscript 6 现在是否已在 Node.js 中得到支持?我也试过运行:
node --harmony app.js
但这给了我一个:
node: bad option: --harmony
在我的 Windows 8 机器上。
我也尝试过使用 string.contains('a');但是那个数组函数也没有定义。
最佳答案
String.prototype.includes
在 Node 中作为 String.prototype.contains
可用。检查一下:http://kangax.github.io/compat-table/es6/ .看标志 26。我认为,最初在标准中提出的名称是 contains
但后来由于兼容性问题而被更改。
V8 拉取请求:https://codereview.chromium.org/742963002
您可以使用 node --v8-options | 检查您的 Node 版本支持哪些选项grep 危害
。确保您使用的是 Node 的更新版本。
这是 Node 版本 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))
从输出中可以看出,当使用 harmony
选项时,箭头字符串 (=>)、字符串和数组被启用。
如果你想充分享受 ES6,我建议使用 babel
和 babel-node
。 String.prototype.includes
得到正确支持。
关于node.js - string.includes ('a' ) 函数未在 Node.js 0.12 中定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30441044/
我是一名优秀的程序员,十分优秀!