javascript - indexOf 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31599012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript - indexOf is not a funciton
提问by 8-Bit Borges
I have this event...
我有这个活动...
<textarea id="chat"> </textarea>
<button type="button" onclick="play_song();">talk</button>
... triggering the following function
:
...触发以下内容function
:
var input = function() {
var chat = document.getElementById("chat").value.split(" ");
return chat && console.log(chat);
}
then there's this function
:
然后是这个function
:
function setIntersection(a, b) {
var result = [];
for (var i = 0; i < a.length; i++) {
if (b.indexOf(a[i]) !== -1 && result.indexOf(a[i]) === -1) {
result.push(a[i]);
}
}
return result;
}
a prototype function
:
一个prototype function
:
Song.prototype.lyricsIntersect = function(input) {
var bestSong = null;
var bestCount = -Infinity;
for (var i in songs) {
var currentCount = setIntersection(songs[i].lyrics, input).length;
if (currentCount > bestCount) {
bestSong = songs[i];
bestCount = currentCount;
}
}
return bestSong && bestSong.name;
}
the code ends here:
代码到此结束:
function play_song() {
var id = Song.prototype.lyricsIntersect(input);
var element = document.getElementById(id);
element.play();
}
but console.log
returns: Uncaught TypeError: b.indexOf is not a function
但console.log
返回:Uncaught TypeError: b.indexOf is not a function
if I test var input = ["one", "two"];
, however, I get the intersection done on the code depending on input
.
var input = ["one", "two"];
但是,如果我测试,我会根据input
.
what am I missing?
我错过了什么?
采纳答案by Jaromanda X
var currentCount = setIntersection(songs[i].lyrics, input).length;
should be
应该
var currentCount = setIntersection(songs[i].lyrics, input()).length;
this also relies on input being corrected as follows
这也依赖于如下更正的输入
var input = function() {
var chat = document.getElementById("chat").value.split(" ");
return chat;
}
回答by RobG
what am I missing?
我错过了什么?
Here, you declare inputas a global and assign a function:
在这里,您将输入声明为全局并分配一个函数:
var input = function() {
var chat = document.getElementById("chat").value.split(" ");
return chat && console.log(chat);
}
I much prefer function declarations, but each to their own.
我更喜欢函数声明,但每个都有自己的。
Here you pass inputas a parameter to Song.prototype.lyricsIntersect
在这里,您将输入作为参数传递给Song.prototype.lyricsIntersect
var id = Song.prototype.lyricsIntersect(input);
which assigns it to its own variable input:
它将它分配给它自己的变量input:
Song.prototype.lyricsIntersect = function(input) {
and then calls setIntersection:
然后调用setIntersection:
var currentCount = setIntersection(songs[i].lyrics, input).length;
Then in setIntersectionit's assigned to the bparameter:
然后在setIntersection 中它被分配给b参数:
function setIntersection(a, b) {
and treated like an array:
并像数组一样对待:
if (b.indexOf(a[i]) !== -1 && result.indexOf(a[i]) === -1) {
回答by kemicofa ghost
For you to be able to use indexOf
function of Array
object then your b
variable has to be an array
. Check your b
type.
为了能够使用对象的indexOf
函数,Array
那么您的b
变量必须是array
. 检查您的b
类型。
Here is the doc regarding Array.prototype.indexOf()function
这是关于 Array.prototype.indexOf()函数的文档
EDIT:
编辑:
With the information you have provided it seems your b
is a reference to input
. You need to make sure input
is an array
. In the case you have provided.. input
is actually a function and not an array
.
根据您提供的信息,您似乎b
是对input
. 您需要确保input
是一个array
. 在您提供的情况下,..input
实际上是一个函数而不是array
.
回答by Vidul
Try to simplify the example as much as possible, your function (as already noted) is not invoked with the expected arguments (presumably wrong logic). As for the indexOf
method:
尝试尽可能简化示例,您的函数(如前所述)不会使用预期的参数(可能是错误的逻辑)调用。至于indexOf
方法:
typeof [].indexOf // "function"
typeof ''.indexOf // "function"
Any data type which implements the iteration protocolshould support indexOf
.
任何实现迭代协议的数据类型都应该支持indexOf
.