Node.js中的startsWith:TypeError:undefined不是函数
我在Node.js中使用startsWith时出错 脚本sw.jsNode.js中的startsWith:TypeError:undefined不是函数,node.js,Node.js,我在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', 1
//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
您的节点版本不支持ECMAScript 6功能,例如
String.prototype.startsWith()
。在写这个时候,节点的最新版本是65.2/7.3.0,所以你应该考虑把节点升级到最新版本,因为你的版本已经很旧了。p>
但是,如果您不可能这样做,您可以使用polyfill:
startsWith
仅在es6中可用,您的节点版本不支持es6。尝试将您的节点升级到节点v6(当前的LTS),它应该可以工作
node -v
v0.12.8
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}