学习reactjs的tutorial是看到function(){this.XX }.bind(this);不是很理解。
补充: bind的作用我懂,在这里的写法的目的不太了解
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
bind是返回一个绑定this后的函数,具体见这个问题
bind的实现
传送门 函数的具体用法可以去mdn上查一下。简单来说就是用来修改bind()前的函数内部this变量指向。
我感觉不bind的话,方法内部的
this应该是配置对象吧就$.ajax({这个对象}),bind传入的this应该是组件。你console输出一下。改变this的指向,类似以前大家很喜欢写的var that = this;或者var self = this;
都是一个道理