我注意到this
仅在init函数和init中调用的函数中使用以下代码(例如:onSearchClick)。任何人都可以解释为什么this
无法在onSearchClick函数中工作?这种JavaScript模式叫做什么?
(function() {
var e,
Search = {
ele: {
sb : $('.searchSubmit'),
st : $('.searchText')
},
vars: {
debug: true
},
init : function() {
e = this.ele;
this.addEvents();
},
addEvents : function() {
this.ele.sb.bind('click', this.onSearchClick);
//Search.ele.sb.bind('click', this.onSearchClick);
//Both, this. and Search. work
},
onSearchClick : function() {
Search.log('onSearchClick: - ');
//this.log('onSearchClick: - ');
//Only Search. works, this. not
},
log : function(p_sMessage) {
if(!Search.vars.debug) { return; }
else { if(window.console) { console.log(p_sMessage); }}
}
};
window.Search = Search;
window.e = e;
}());
Search.init();