reactjs bind()的使用场景

原创
2017/11/14 10:35
阅读数 137
  1. 传递附加参数 
render: function() {
    return _.map(list, function(item) {
        return <li onClick={this.clickItem.bind(this, item)}>{item.name}</li>;
    });
},
clickItem: function(item, event) {
    //do something with the clicked item
}

2、

this.x = 9;    // this refers to global "window" object here in the browser
var module = {
  x: 81,
  getX: function() { return this.x; }
};

module.getX(); // 81

var retrieveX = module.getX;
retrieveX();   
// returns 9 - The function gets invoked at the global scope

// Create a new function with 'this' bound to module
// New programmers might confuse the
// global var x with module's property x
var boundGetX = retrieveX.bind(module);
boundGetX(); // 81

参考

ReactJS 已经不自动Bind了,要手工绑定

语法手册

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部