突然间的this

来源:互联网 发布:电脑检测不到网络 编辑:程序博客网 时间:2024/05/16 18:36

在页面的时候报个奇怪的dataHandle is undefined

dataHandle: function() {//},simpleCategoryList: function() {this.dataHandle();}

在view 里面render 的时候用到, 本想先一次声明后面可以懒写个"this."

render: function() {var _this = this,categories,simpleCategoryList = this.simpleCategoryList;categories = simpleCategoryList(); }

稍注意一下,才发现  this in simpleCategoryList is window , 看来偷懒不得, 会心一笑。

render: function() {var _this = this,categories;categories = this.simpleCategoryList();}

还有另外一种方法,不过这种就超偏离原来的意图了。So ugly.

render: function() {var _this = this,categories;categories = simpleCategoryList(_this); // so ungly}

在simpleCategoryList里面还要接收_this再缓存起来,不要也罢:)











原创粉丝点击