日期:2014-05-16  浏览次数:20380 次

javascript类中的属性值
这样一个类(之前引用了prototype.js):
function testClass()
{
this.test=1;

this.get = function()
{
var url='http://*.*.*.*/response.aspx';
var pars='';
var myAjax = new Ajax.Request(url,{method: 'get',parameters: pars,onComplete: this.showResponse});this.test=2;
}

this.showResponse = function(request)
{
var xml = request.responseXML;
var list = xml.selectNodes( "/Categories/Category"); this.test=3;
alert(this.test);
}
}

在之后的代码中
var c = new testClass();
c.get();
document.write(c.test);

alert出来的结果是undifined,而document.write的结果是2,也就是说this.showResponse方法中this.test访问的不是类中定义的test属性,不知道哪位高手能否指点一二,不胜感激!

我的目的是想在showResponse方法中解析返回的XML,并将解析的结果赋值给共有属性,最后在随后的代码中用class.property的方法访问得到的结果。

------解决方案--------------------
this.showResponse = function(request) 

var xml = request.responseXML; 
var list = xml.selectNodes( "/Categories/Category"); this.test=3; 
alert(this.test); 

在这里面this发生了变化并非是c了.所以没有test这个属性.
this.get = function() 

var url='http://*.*.*.*/response.aspx'; 
var pars=''; 
var myAjax = new Ajax.Request(url,{method: 'get',parameters: pars,onComplete: this.showResponse(request,this)});this.test=2; 


this.showResponse = function(request,o) 

var xml = request.responseXML; 
var list = xml.selectNodes( "/Categories/Category"); o.test=3; 
alert(o.test); 

改成这样试试