gpt4 book ai didi

node.js - Node.js 中的开头为 : TypeError: undefined is not a function

转载 作者:行者123 更新时间:2023-12-02 12:58:37 25 4
gpt4 key购买 nike

我在 Node.js 中使用startsWith 时遇到错误

脚本sw.js

//startswith
var str = 'Sein oder nicht sein, dass ist hier die Frage'; console.log(str.startsWith('Sein oder'));
// true
console.log(str.startsWith('nicht sein'));
// false
console.log(str.startsWith('nicht sein', 10));
// true

脚本的输出:

/Users/.../sw.js:2
= 'Sein oder nicht sein, dass ist hier die Frage'; console.log(str.startsWith
^
TypeError: undefined is not a function
at Object.<anonymous> (/Users/.../sw.js:2:76)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

Node 版本(不确定是否相关)

node -v
v0.12.8

最佳答案

您的 Node 版本不支持 ecmascript6 功能,例如 String.prototype.startsWith()。在撰写本文时,Node 的最新版本是 6.9.2/7.3.0,因此您应该考虑将 Node 升级到最新版本,因为您的版本已经很旧了。

但是,如果这对您来说不可能,您可以使用polyfill found on mdn :

if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}

关于node.js - Node.js 中的开头为 : TypeError: undefined is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41268600/

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