我正在尝试编写一个简单的 javascript(以及 coffeescript)测试并且有一些令人困惑的东西:
dclib.appendVarToUrl = (base, k, v) ->
check base, String
console.log("base is a:", typeof(base))
if base.includes("?")
...
// called with
dclib.appendVarToUrl("some/url", "score", 5)
给我:
base is a: string
TypeError: Object some/url has no method 'includes'
那么为什么 String 没有 .includes() 方法呢? 这个 coffeescript 是否以一种奇怪的方式包装了一个对象?
最佳答案
啊哈,看起来我的浏览器支持这个作为 ES6 方法,但我使用的节点版本不支持。我在服务器上运行单元测试,而该方法在客户端运行良好。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
关于javascript - 什么时候字符串不是字符串?当它没有 includes() 方法时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29089959/