Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我怎样才能避免不是一个函数错误?_Javascript_Reactjs - Fatal编程技术网

Javascript 我怎样才能避免不是一个函数错误?

Javascript 我怎样才能避免不是一个函数错误?,javascript,reactjs,Javascript,Reactjs,我在ES6模式下的reactJs应用程序中使用switch语句。我有以下声明: switch (hoera) { case 'one': return this.runThis(); break; default: } runThis(something) { .. } chromeconsole错误为: TypeError: this.runThis is not a function 所以r

我在ES6模式下的reactJs应用程序中使用switch语句。我有以下声明:

switch (hoera) {
        case 'one':
            return this.runThis();
            break;
        default:
}

runThis(something)
{
        ..
}
chromeconsole错误为:

TypeError: this.runThis is not a function

所以runThis是在我的组件上定义的方法。但似乎在switchstatement之外工作。

'this'并不是指函数中包含
switch
语句的组件实例

您可以在构造函数中添加
this.myFunction=this.myFunction.bind(this)
,其中
myFunction
是包含switch语句的函数

下面是一篇很好的文章,介绍了如何在react内部处理
此问题的更多方法:


这里有一些关于
bind
的文档:

为什么不添加
console.log(this,this.runThis)
,这样你就可以找到它了。你需要提供更多的上下文。但是我想它是一个复制品,我实际上认为需要绑定的不是
runThis
,而是包含开关的函数(未显示)。“在switch语句中,这是失去对类的绑定。”这部分是错误的。