简体   繁体   English

myFunction不是函数

[英]myFunction is not a function

I want to call a function and have an error message "this.testFunc is not a function" 我想调用一个函数,并显示一条错误消息“ this.testFunc不是一个函数”

I think the problem is that the function is initialised in ComponentDidMount and then testFunc is not defined yet, but I don't know how to solve it. 我认为问题在于该函数是在ComponentDidMount中初始化的,然后尚未定义testFunc,但是我不知道如何解决它。

var _receivedNb = 0

export default class App extends Component {
  constructor(){
    super()
  }

  componentDidMount() {

    BleManager.start({showAlert: false});

    this.handlerUpdate = bleManagerEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', this.handleUpdateValueForCharacteristic );
  }


  handleUpdateValueForCharacteristic(data) {
      _receivedNb = this.testFunc();
  }

  testFunc() {
    var r = 2;
    return r;
  }

You have to bind your event handler 您必须绑定事件处理程序

constructor(){
  super()
  this.handleUpdateValueForCharacteristic = this.handleUpdateValueForCharacteristic.bind(this)
}

You should bind that function in constructor. 您应该在构造函数中绑定该函数。 I hope following code will help, 希望以下代码对您有所帮助,

var _receivedNb = 0

export default class App extends Component {
  constructor(props){
    super(props)
    this.testFunc = this.testFunc.bind(this);
  }

  testFunc() {
    var r = 2;
    return r;
  }
  componentDidMount() {

    BleManager.start({showAlert: false});

    this.handlerUpdate = bleManagerEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', this.handleUpdateValueForCharacteristic );
  }


  handleUpdateValueForCharacteristic(data) {
      _receivedNb = this.testFunc();
  }

declare the function handleUpdateValueForCharacteristic as an arrow function in order to let it inherit the outer scope (that is the class scope) and you should be fine. 将函数handleUpdateValueForCharacteristic声明为箭头函数,以使其继承外部范围(即类范围),您应该会handleUpdateValueForCharacteristic

handleUpdateValueForCharacteristic = data => {
  _receivedNb = this.testFunc();
}

It is because of this reference inside handleUpdateValueForCharacteristic is not what you are thinking. 这是因为handleUpdateValueForCharacteristicthis引用不是您在想的。 You can learn about this in detail from You Dont Know Js 您可以从“ 您不知道Js”中详细了解这一点。

Coming to the solution there are multiple approaches to resolve this, you need to fix the this reference inside handleUpdateValueForCharacteristic 来到解决方案中,有多种方法可以解决此问题,您需要在handleUpdateValueForCharacteristic内部修复this引用。

One easy approach is using ARROW FUNCTION => . 一种简单的方法是使用ARROW FUNCTION => Arrow functions preserve this from the location where the function is defined. 箭头函数从定义函数的位置保留this函数。

var _receivedNb = 0

export default class App extends Component {
    constructor(){
        super()
    }

componentDidMount() {

    BleManager.start({showAlert: false});

    this.handlerUpdate = bleManagerEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', this.handleUpdateValueForCharacteristic );
}


handleUpdateValueForCharacteristic = (data) => {
    _receivedNb = this.testFunc();
}

testFunc = () => {
  var r = 2;
  return r;
}

Second approach is using .call, .bind or .apply . 第二种方法是使用.call,.bind或.apply You can read about them later, In short bind takes this in params and return a hard-binded function with this always referring to what was provided inside .bind . 稍后您可以阅读有关它们的信息。总之,bind将this引入参数中,并返回一个硬绑定函数,始终引用.bind中提供的内容。 You can simply use handleUpdateValueForCharacteristic.bind(this) and call it. 您可以简单地使用handleUpdateValueForCharacteristic.bind(this)并对其进行调用。 The best way to do this is inside constructor . 最好的方法是在constructor内部。

var _receivedNb = 0

export default class App extends Component {
    constructor(){
        super()  
        this.newHandleUpdateValueForCharacteristic = handleUpdateValueForCharacteristic.bind(this)

    }

componentDidMount() {

    BleManager.start({showAlert: false});

    this.handlerUpdate = bleManagerEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', this.handleUpdateValueForCharacteristic );
}


handleUpdateValueForCharacteristic = (data) => {
    _receivedNb = this.testFunc();
}

testFunc = () => {
  var r = 2;
  return r;
}

and use newHandleUpdateValueForCharacteristic every where else. 并在其他任何地方使用newHandleUpdateValueForCharacteristic

Call and apply are similar to bind with only difference that it immediately calls the function with this reference and other required parameters passed inside call and apply. 调用和应用类似于绑定,只是区别在于它立即使用此引用和在调用和应用内部传递的其他必需参数立即调用该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM