1

無名関数ハンドラー内で fx2 から fx1 にアクセスできませんか?

var MyComponent = function () {
    //my constructor
}

MyComponent.prototype.fx1 = function() { //code }

MyComponent.prototype.fx2 = function() {

    var btn1 = document.getElementById('Button1')

    btn1.onclick = function() {
        //issue is here
        //trying to call fx1 from here but can't access it.

        this.fx1();  //doesn't work. 
    }
}
4

2 に答える 2

1

これを行う別の方法:

MyComponent.prototype.fx2 = function() {
    var btn1 = document.getElementById('Button1');
    btn1.onclick = (function() {
        this.fx1();
    }).bind(this);
}
于 2013-10-16T18:21:00.373 に答える