'这个'参考不起作用

时间:2014-10-10 05:02:12

标签: javascript jquery

我注意到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();

0 个答案:

没有答案