节点v0.10.25中的字符串原型没有' endsWith'

时间:2014-12-22 09:05:32

标签: javascript node.js

当我尝试在节点服务器版本v0.10.25中使用endsWith检查字符串模式时,它引发了错误,

Object ''''''' has no method 'endsWith'

然后我从这个链接https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith发现,String.prototype.endsWith只来自ECMA6。 那么,ecma版本节点v0.10.25实现了什么?未来发布的nodejs,我可以期待ECMA6兼容吗?

2 个答案:

答案 0 :(得分:5)

很明显,如果不是ES6,它将在ES5或jaavacript的当前迭代中实现。而不是等待它,你可以编写自己的

String.prototype.endsWith = String.prototype.endsWith || function(str){
   return new RegExp(str + "$").test(str);
}

答案 1 :(得分:3)

http://kangax.github.io/compat-table/es6/您可以在此处找到ecma-script-6的兼容性图表。

并阅读此答案https://stackoverflow.com/a/13352093/3556874。 您可以通过--harmony app.js方式激活节点和声标志节点,以使节点与字符串endsWith兼容