【发布时间】:2015-05-21 08:36:51
【问题描述】:
最近我在用 JavaScript 写逻辑,写了这样的东西
var str="hello world";
if(str.contains("w"))
//do something
else
//do anotherthing
在我在 Chrome 中运行该页面之前,我认为它运行良好。在 Chrome 中,我收到contains is not a function 的错误。
虽然我通过将逻辑修改为
来摆脱它var str="hello world";
if(str.indexOf("w")!=-1)
//do something
else
//do another thing
contains 不是标准的 ECMAScript 函数吗?我可以在 Firefox 中通过智能感知看到 contains,但在 Chrome 中看不到。
在不同的浏览器中测试这些时,我在控制台中注意到
String.subString/indexOf //not showing in chrome but works in Firefox
而不是 str.substring/indexOf 在 chrome 中工作
这些方法不是标准String 对象的一部分吗?
【问题讨论】:
-
.includes是方法的标准名称。.contains是首选,但它破坏了网站,因为曾经流行的库 (Mootools) 定义了具有不同语义的字符串的.contains方法。
标签: javascript google-chrome standard-library ecmascript-harmony