【问题标题】:JS Error: <parameter>.contains is not a function, not sure whyJS 错误:<parameter>.contains 不是函数,不知道为什么
【发布时间】:2019-07-03 18:36:59
【问题描述】:

我一直在写一些 JavaScript 代码,并介绍了这个小函数:

function decodeLink(thelink) {
    console.log(typeof(thelink)); // Reports 'string'

    if (thelink.contains("something")) {
        // Cool condition
    }
}

但是,如果我打电话给decodeLink("hello");,我会收到以下错误:

TypeError: thelink.contains is not a function

请注意,我使用的是 node.js 和 discord.js,但是将导入注释掉不会产生任何结果。

我已经习惯了强类型的 C# 编程风格,这种弱类型对我来说还是很陌生的。我确定我错过了一些重要的东西(例如一些明确的方式告诉程序它正在处理一个字符串),但是没有搜索让我更接近于什么......

【问题讨论】:

标签: javascript string contains


【解决方案1】:

你需要的方法叫includes not contains

function decodeLink(thelink) {
    console.log(typeof(thelink)); // Reports 'string'

    if (thelink.includes("something")) {
        // Cool condition
    }
}

【讨论】:

  • 当然它是一个不同的关键字,我只是假设它与 C# 相同...我一定会在 10 分钟内将其标记为答案。再次非常感谢你:)
猜你喜欢
  • 2011-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多