This probably depends a bit on what should be preferred. I am inclined to use the latter (although in fact I prefer the former, but some third-party libraries we use the restriction). I think the important thing when choosing a style is consistent.
prototype.bind , IE8 , .
, , , bind , , , , . jsperf, , .
, JSPerf . , , , ( ). , 7 . , .
var limit = 100000;
var a = {
val: 0,
do: function(i) { val = i; }
};
var b = {
myFunc: function(callback) { callback(); }
};
var start = +new Date();
for(var i = 0; i < limit; i++) {
b.myFunc(function() {
this.do(i);
}.bind(a));
};
var end = +new Date();
var diff = end - start;
console.log("bind took " + diff + " ms");
var start = +new Date();
for(var i = 0; i < limit; i++) {
var that = a;
b.myFunc(function() {
that.do(i);
});
};
var end = +new Date();
var diff = end - start;
console.log("closured took " + diff + " ms");
Hide result