从jQuery'close'访问外部方法

时间:2012-01-24 23:59:11

标签: javascript jquery

我想在点击处理程序中使用fnB。有没有优雅的解决方案?感谢。

A = function() {
   ...
}

B = function() {
   ...
}
B.prototype = new A();
B.prototype.fnB = function() {}
B.prototype.fn = function() {
  // this isntanceof B -> true
  jQuery(selector).click(function() {
    this.fnB() // this instance of B -> false, this == $(selector) -> true
  }
}

基于SoWeLie答案的实施:

EventType = {
    ButtonClicked: 0
    ...
}

function Delegate(object, method) {
    return function() { method.apply(object, arguments); }
}

function Observable() {
    this._eventHandlers = {};
}

Observable.prototype = {
    addListener: function(eventType, listener) {
        if(!this._eventHandlers.eventType) {
            this._eventHandlers.eventType = [];
        }
        this._eventHandlers.eventType.push(listener);
    },

    removeListener: function(eventType, listener) {
        if(this._eventHandlers.eventType) {
            for(var i = this._eventHandlers.eventType.length; --i > -1;) {
                if(!listener || this._eventHandlers.eventType[i] == listener) {
                    this._eventHandlers.eventType.splice(i, 1);
                }
            }
        }
    },

    fireEvent: function(eventType, data) {
        if(this._eventHandlers.eventType) {
            for(var i = 0; i < this._eventHandlers.eventType.length; ++i) {
                var handleEvent = this._eventHandlers.eventType[i];
                if(handleEvent) {
                    handleEvent(this, data);
                }
            }
        }
    }
}

Dialog.Buttons = { ButtonOk: 0, ButtonCancel: 1, ButtonYes: 2, ButtonNo: 3, ButtonClose: 4 };
Dialog.prototype = new Observable();
Dialog.prototype.bindEvents = function() {
    $('#dlgButtonOk', this._$el).click(Delegate(this, function() {
        this.fireEvent(EventType.ButtonClicked, Dialog.Buttons.ButtonOk );
    }));
    $('#dlgButtonCancel', this._$el).click(this, function() {
        this.fireEvent(EventType.ButtonClicked, Dialog.Buttons.ButtonCancel );
    });

$(function() {
    dialog = new Dialog('.dialogWnd')
    dialog.addListener(EventType.ButtonClicked, function() {alert(1)})
});

???? 哎呀!无法提交您的修改,因为:

您的帖子没有太多上下文来解释代码部分;请更清楚地解释您的情景。 ???

3 个答案:

答案 0 :(得分:1)

var that = this;
jQuery(selector).click(function() {
  that.fnB()
}

或者您可以使用bind,但它尚未得到广泛支持。

答案 1 :(得分:1)

最优雅的解决方案是使用函数的apply方法:

var hitch = function (scope, callback) {
        return function () {
            return callback.apply(scope, Array.prototype.slice.call(arguments));
        }
    }

更改您的代码:

jQuery(selector).click(hitch(this, function() {
    this.fnB() // this instance of B -> false, this == $(selector) -> true
  }))

注意在匿名函数上调用hitch,这将确保在单击处理程序中保留cope。

答案 2 :(得分:0)

只需使用闭包:

B.prototype.fn = function() {
    // this isntanceof B -> true
    var that = this;
    jQuery(selector).click(function() {
        that.fnB();
    });
};