.bind(this)vs var context =这在Javascript中 - 什么是最好的风格?

时间:2015-10-28 05:20:32

标签: javascript asynchronous callback this

这是一个风格问题,也许是主观的。

哪个更适合用于可读的标准代码?

A:" var context = this"

var object = {
  foo: 'bar',
  method: function(){  
    $.get('/some/async/api', (function (req, res){
      console.log(this.foo);  //==> bar
    }).bind(this));
  }
}

B:" .bind(this)"

WebView webView = new WebView(this);
webView.loadUrl("http://www.google.com/");
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setView(webView);
dialog.setPositiveButton("Okay", null);
dialog.show();

1 个答案:

答案 0 :(得分:0)

我更喜欢第一种格式,因为它简单而有效。 zone.js是一个很棒的库,可用于范围界定。