gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 00:23:58 24 4
gpt4 key购买 nike

我确实了解 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,我建议使用 babelbabel-nodeString.prototype.includes 得到正确支持。

关于node.js - string.includes ('a' ) 函数未在 Node.js 0.12 中定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30441044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com