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

String prototype in node v0.10.25 does not have 'endsWith'

本文关键字:endsWith 原型 字符串 v0 节点      更新时间:2023-09-26

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

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?

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

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

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

并 https://stackoverflow.com/a/13352093/3556874 阅读此答案。你可以这样激活节点和谐标志节点--harmony app.js,使节点与字符串兼容endsWith