2016-06-15 3 views
0

こんにちは私は反応の初心者です。this._method(...)のメソッドを呼び出すときにエラーが発生しました。バインドは関数ではありません。

はTypeError::私はそれは私に次のエラーがスローされます私のブラウザ上でそれを開こうとすると

var React = require('react') 
var ReactDom = require('react-dom') 



class TestText extends React.Component { 

    render() { 
     const number1 = 6 
     const number2 = 5 

     return (
      <div> 
       <h3>Hello World</h3> 
       <p> The result of multiply {number1} and {number2} is {this._multiply(number1,number2).bind(this)}</p> 
      </div> 
     ) 
    } 

    _multiply (num1, num2) { 
     return num1*num2 
    } 
} 

ReactDom.render(<TestText />, document.getElementById('test-container')) 

:それはこのようになります。this._multiply(...)バインド機能

ではありません

これに関するお手伝いはありますか?ありがとうございました!

+0

'this._multiply(数値1、数値2)は'ではありませんあなたはそれにバインドすることはできませんので、数字です。 – zerkms

答えて

0

まあ、

  • this._multiplyバインドを適用することができ、関数の参照、です。

  • this._multiply(a, b)は、結果である数値を返します。

this._multiply.bind(this, a, b)()を使用しても問題ありません。 カッコを簡略化してthis._multiply.call(this, a, b)を使用できます。 しかしthis._multiplyは、関数内の任意のthisの参照を使用していないので、そこでは、すべてのバインドする必要はなく、あなたは、単に入力できます

{this._multiply(a, b)}

+0

ありがとうございました!しかしthis._multiply.call(this、a、b)は何も返さないので、大括弧でthis._multiply.call(this、a、b)()を呼び出さなければなりません。 – LuisPinto

+0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/callはES6のことですか? – jsdario

関連する問題