【问题标题】:Right-hand side of instanceof is not callableinstanceof 的右侧不可调用
【发布时间】:2017-09-16 01:41:17
【问题描述】:

所以,我正在尝试使用 javascript 制作一个简单的 Discord Bot,并且我想检测玩家用户名是否在禁止列表中。 我有一个数组

var banned = ['Andrew','David']

还有一个如果

if (message.author.username instanceof banned) {.....

但是当我运行它时,它会输出

if (message.author.username instanceof banned)
                            ^

TypeError: Right-hand side of 'instanceof' is not callable

我能做什么?

【问题讨论】:

  • 你想做什么?那是对instanceof的不当使用
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 与您正在做的事情无关 - 投票结束为 此问题是由无法再复制的问题或简单的印刷错误引起的。虽然类似的问题可能是这里的主题,但这个问题的解决方式不太可能帮助未来的读者。这通常可以通过在发布之前确定并仔细检查重现问题所需的最短程序来避免。

标签: javascript


【解决方案1】:

这不是instanceof 的用途。 instanceof 用于查看对象是否是特定构造函数的实例(例如:banned instanceof Array)。

如果只想查看某个元素是否在数组中,可以使用.indexOf()

if(banned.indexOf(message.author.username) != -1)

【讨论】:

  • 我想补充一点,Array.includes() 是更惯用和可读的变体。现在几乎所有地方都支持!
【解决方案2】:

instanceof 用于检查对象是否是类的实例。你想要做的是检查一个字符串是否在这样的数组中:

if (banned.indexOf(message.author.username) >= 0) {...

【讨论】:

    【解决方案3】:

    我在 vue / nuxt js 上遇到了同样的错误。

    问题是道具类型设置错误:

    blog: {
      type: {},
      required: false,
    },
    

    正确的做法是设置类型 Object {}

    blog: {
      type: Object,
      required: false,
    },
    

    【讨论】:

      【解决方案4】:

      instanceof 用于查看对象是否属于特定类型。您正在尝试查看对象是否是数组的成员。

      试试 Array 对象的includesindexOf 方法。

      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

      【讨论】:

        【解决方案5】:

        我知道我来晚了,但在代码可维护性和对代码的第一印象方面,我肯定会考虑 includes()

        ES2016 规范 包括 includes() 用于数组数据结构。 includes() 检查数组是否包含某个元素,根据需要返回truefalse

        但在 ES5 中,我们习惯于使用 indexOf() 方法执行此类操作。

        为什么要使用includes

        1. includes 方法可以找到NaNundefined,而indexOf 方法则不能。 在数组中查找NAN 的示例:

          const  array = [NaN];
          if (array.indexOf(NaN) == -1){
              console.log("NaN not found in the array");//NaN not found in the array
          }
          
          const  array1 = [NaN];
          
          if (array1.includes(NaN)){
              console.log("true. NAN was found in the array");// true. NAN was found in the array
          }
          

          在数组中查找undefined 的示例:

          const array = [, , , ,];
          
          if(array.includes(undefined)){
              console.log("true array elements are undefined");// true array elements are undefined
          } 
          
          const array1 = [, , , ,];
          
          if(!array1.indexOf(undefined) == -1 ){
              console.log("true. array elements are undefined");
          }else {
              console.log("Sorry can't find undefined");// Sorry can't find undefined
          }
          
        2. includes 方法不区分 -0+0(这不是错误,但很清楚 javascript 的工作原理。

           const a = [-0].includes(+0);
           console.log(a);//true
          

        在很多情况下,我看到indexOfinclude 快一点。这取决于您需要妥协的地方。此外,与 lodash 的 includes 方法相比,ES6 includes 的性能非常快。

        Performance comparison source.

        【讨论】:

          猜你喜欢
          • 2017-01-18
          • 2019-09-17
          • 2018-01-11
          • 2017-08-28
          • 2020-04-05
          • 1970-01-01
          • 2023-01-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多