2016-05-23 1 views
1

私はjsオブジェクトを持っています。私は別のメソッドを呼び出して約束を返しますが、.then()の中からメンバー関数foo()にアクセスすることはできません。なぜfoo()にアクセスできないのですか?どのようにアクセスできますか?ここに私のコードは次のとおりです。この上の任意の助けを約束の中からオブジェクトのメソッドをどのように呼び出すのですか?()?

function LoadImageGroupFormat() { 
 
    return new Promise(function(resolve, reject) { 
 
     var xhttp = new XMLHttpRequest(); 
 
     xhttp.open('GET', "imagegroup_format.txt"); 
 
     xhttp.onload = function() { 
 
     if (xhttp.status == 200) resolve(xhttp.responseText); 
 
     else reject(Error(xhttp.statusText)); 
 
     }; 
 
     xhttp.onerror = function() { 
 
     reject(Error("Network Error")); 
 
     }; 
 
     xhttp.send(); 
 
    }); 
 
    } 
 
    //the object 
 
var Registerhandler = { 
 
    imageGroupIndex: 0, 
 

 
    foo: function() { 
 
    //Do something else here 
 
    }, 
 

 
    GetImageGroupFormat: function() { 
 
    var imageGroupIndex = this.imageGroupIndex; 
 
    LoadImageGroupFormat().then(function(ImageGroup_Format) { 
 
     //Do something with ImageGroup_Format 
 
     imageGroupIndex++; //This works 
 
     foo(); //Doesn't work - undefined 
 
    }, function(error) { 
 
     console.error("Failed to Load ImageGroup Format", error); 
 
    }); 
 
    } 
 
}

感謝。

+1

'imageGroupIndex ++;' "作品" あなたはローカル変数 'imageGroupIndex'を作ったので、それはRegisterhandler.imageGroupIndex' –

+0

がどのように私は、インスタンスの変数を変更します'修正イマイチ? –

答えて

1

fooは財産ではなく、機能/変数名では、プロパティの構文を使用して、それを呼び出す必要があります。 thisはクロージャーに保存されていないので、クロージャーを保持するローカルクロージャー変数を定義する必要があります。

またそうでなければ、値のコピーをインクリメントしている、そのプロパティを更新するためにself.imageGroupIndexを使用する必要があります。

var Registerhandler = { 
    imageGroupIndex: 0, 

    foo: function() { 
    //Do something else here 
    }, 

    GetImageGroupFormat: function() { 
    var self = this; 
    LoadImageGroupFormat().then(function(ImageGroup_Format) { 
     //Do something with ImageGroup_Format 
     self.imageGroupIndex++; 
     self.foo(); 
    }, function(error) { 
     console.error("Failed to Load ImageGroup Format", error); 
    }); 
    } 
} 
1

使用Function.prototype.bind()bind()メソッドが呼び出されたときに、新しい関数が呼び出されたときに提供される任意の前の引数の指定された配列と、与えられた値に設定し、そのthisキーワードを有し、新たな機能を作成します。

function LoadImageGroupFormat() { 
 
    return new Promise(function(resolve, reject) { 
 
    resolve('success!'); //updated fro demonstration purpose 
 
    }); 
 
} 
 
var Registerhandler = { 
 
    imageGroupIndex: 0, 
 
    foo: function() { 
 
    alert('In foo!'); 
 
    }, 
 

 
    GetImageGroupFormat: function() { 
 
    var imageGroupIndex = this.imageGroupIndex; 
 
    LoadImageGroupFormat().then(function(ImageGroup_Format) { 
 
     console.log(ImageGroup_Format); 
 
     this.imageGroupIndex++; 
 
     this.foo(); 
 
    }.bind(this), function(error) { 
 
     console.error("Failed to Load ImageGroup Format", error); 
 
    }); 
 
    } 
 
} 
 
Registerhandler.GetImageGroupFormat();

+0

私はそれがうまくいくとは思わない。 '.bind'を呼び出すとき、あなたはオブジェクトのメソッドにないので、' this'はオブジェクトに設定されません。 – Barmar

+0

@Barmar、私はデモを提供しました..それはそれが失敗することを知ってみましょう.. – Rayon

+0

@Barmarはいです。 'bind'は' GetImageGroupFormat'メソッドで直接呼び出されます。 –

関連する問題