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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:53:29  来源:igfitidea点击:

javascript - indexOf is not a funciton

javascript

提问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.logreturns: 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 indexOffunction of Arrayobject then your bvariable has to be an array. Check your btype.

为了能够使用对象的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 bis a reference to input. You need to make sure inputis an array. In the case you have provided.. inputis 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 indexOfmethod:

尝试尽可能简化示例,您的函数(如前所述)不会使用预期的参数(可能是错误的逻辑)调用。至于indexOf方法:

typeof [].indexOf // "function"

typeof ''.indexOf // "function"

Any data type which implements the iteration protocolshould support indexOf.

任何实现迭代协议的数据类型都应该支持indexOf.