哪种方法最佳,哪种方式可以获得更好的性能?
更新:jsperf.com报道(a)更快@ http://jsperf.com/closure-vs-global-variable
a)使用封闭物
var obj = {
init: function() {
var self = this;
$('#element').click(function() {
self.clickEvent();
});
},
clickEvent: function() {
this.miscMethod();
},
miscMethod: function() {}
};
Run Code Online (Sandbox Code Playgroud)
b)使用全局变量
var obj = {
init: function() {
// removed self=this closure
$('#element').click(this.clickEvent); // simple method handler
},
clickEvent: function() {
obj.miscMethod(); // global variable used
},
miscMethod: function() {}
};
Run Code Online (Sandbox Code Playgroud)