そこで、初期化フェーズで AJAX 呼び出しを行ってプロパティを設定するオブジェクトを作成しました。ただし、私は非常に奇妙な動作に遭遇しています: $.ajax() スコープ内でプロパティ値を印刷して表示することはできますが、プロパティの値を返すパブリックメソッドには「未定義」の戻り値があります。
JS コードは次のようになります。
function MyFunction() {
this.myProperty;
this.init();
}
Myfunction.prototype.getPropertyValue = function () {
alert(this.myProperty); // returns 'undefined'
}
Myfunction.prototype.init = function () {
$.ajax({
type: 'get',
url: "getProperty.php",
dataType: "json",
success: function(response) {
this.myProperty = response[0].Property;
alert(this.myProperty) // returns 'Property Name'
}
});
}
私の考えでは、 $.ajax() スコープ内では、「これ」は実際には別のものを参照しています。それで、私の質問は、「this.myProperty」が設定されており、AJAX スコープの外に出たときにその値を失わないようにするにはどうすればよいですか?
どんな助けでも大歓迎です。