我一直在写一些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#编程风格,这种弱类型对我来说是很新的。我确定我错过了一些重要的东西(例如一些明确的方式来告诉程序它正在处理一个字符串(,但没有搜索让我更接近什么......
你需要的方法叫做 包含不包含
function decodeLink(thelink) {
console.log(typeof(thelink)); // Reports 'string'
if (thelink.includes("something")) {
// Cool condition
}
}