Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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 componentDidMount()中“this”的含义,ReactJS_Javascript_Jquery_Reactjs - Fatal编程技术网

Javascript componentDidMount()中“this”的含义,ReactJS

Javascript componentDidMount()中“this”的含义,ReactJS,javascript,jquery,reactjs,Javascript,Jquery,Reactjs,寻求澄清“此”在本上下文中的含义。为什么我需要在ajax请求之后将“this”绑定到回调?当我检查调试器时,不管我是否调用bind,它都会说'this'绑定到构造函数 var BugList = React.createClass({ getInitialState: function() { return { bugs: [] } }, componentDidMount: function() { $.ajax('/api/bugs').done(

寻求澄清“此”在本上下文中的含义。为什么我需要在ajax请求之后将“this”绑定到回调?当我检查调试器时,不管我是否调用bind,它都会说'this'绑定到构造函数

var BugList = React.createClass({
getInitialState: function() {
    return {
        bugs: []
    }
},

componentDidMount: function() {

    $.ajax('/api/bugs').done(function(data) {
        this.setState({
            bugs: JSON.parse(data)
        });
    }.bind(this));
},

这是因为默认情况下,这将指向:

当前执行上下文的ThisBinding的值

由于您的上下文将在回调实际运行期间更改,因此这不会引用您当前的错误列表实例

bindthis本质上声明,无论上下文如何,都将其设置为表示创建此函数的上下文,即错误列表


我建议你看看:,和。有关此概念的更详细说明

你能解释一下-1吗?这个答案没有用,再次单击以撤消相关:相关:当我检查调试器时,它说“This”绑定到构造函数,无论我是否调用bind。-你这是什么意思?您的示例中没有构造函数。你在哪里检查这本书的装订?