日期:2014-05-16 浏览次数:20495 次
function Test() { this.sClassName = "Test"; } Test.prototype.show = function(0 { alert(this.sClassName); // 正常显示Test var callback = function() { alert(this.sClassName); // 显示undefined,如何才能显示Test中的this.sClassName? }; $.ajax({ success: callback }); callback = null; }; var test = new Test(); test.show();
function Test() { this.sClassName = "Test"; } Test.prototype.show = function(0 { alert(this.sClassName); // 正常显示Test var cn = this.sClassName; var callback = function() { alert(cn); // 这样写就可以 }; $.ajax({ success: callback }); callback = null; }; var test = new Test(); test.show();
------解决方案--------------------
要理顺上下文:
function Test() {
this.sClassName = "Test";
}
Test.prototype.show = function(0 {
alert(this.sClassName); // 正常显示Test
var This = this;
var callback = function() {
alert(This.sClassName); // 显示undefined,如何才能显示Test中的this.sClassName?
};
$.ajax({
success: callback
});
callback = null;
};
var test = new Test();
test.show();