关于react的疑惑_3
在菜鸟上学react的时候看到一段代码
<script type="text/babel"> class Hello extends React.Component { constructor(props) { super(props); this.state = {opacity: 1.0}; } componentDidMount() { this.timer = setInterval(function () { var opacity = this.state.opacity; opacity -= .05; if (opacity < 0.1) { opacity = 1.0; } this.setState({ opacity: opacity }); }.bind(this), 100); } render () { return ( <div style={{opacity: this.state.opacity}}> Hello {this.props.name} </div> ); } } ReactDOM.render( <Hello name="world"/>, document.body ); </script>
主要是它里面的
componentDidMount() { this.timer = setInterval(function () { var opacity = this.state.opacity; opacity -= .05; if (opacity < 0.1) { opacity = 1.0; } this.setState({ opacity: opacity }); }.bind(this), 100);//this绑定在这里?????? }
这里this是绑定在这里我就有点疑惑了,因为这个bind是在setInterval里面的而且还是绑定到了里面的函数里,那么这个里面的this不就会变成window么???