晕...居然csdn爆了.....
继续..
在调用的时候则出现了问题:在FunctionB当中无法使用this.PropertyA来获取或设置属性了.
由于源代码太多.我写了一个相似情况demo代码如下:
<script language="javascript">
function A()
{
}
A.prototype.PropertyA = "aaa";
A.prototype.FunctionA = function(a){
   var d=document;if(!a)a=window.event;
   d.onmousedown=function(){
this.PropertyA = "gg";
//注意这句 this.FunctionB("usa"); // 这里是无法找到A的方法FunctionB了.情况类似
alert(this.PropertyA);
   }
}
A.prototype.FunctionB = function(tmp)
{
   this.PropertyA = tmp;
}var a = new A();
a.PropertyA = "tt";
a.FunctionA();
</script>望高手指教..需要解决的问题.我的实际问题其实就是在使用onreadystatechange的时候使用的函数是这个类自身的方法:
this.http_request.onreadystatechange = this.GetMessages;,.想要知道怎么在GetMessages()这个方法当中获取或设置这个类的属性..谢谢.分不够再加

解决方案 »

  1.   

    d.onmousedown=function(){
    this.PropertyA = "gg"; 
    你这样定义的话,这里面的 this 已经不是 A 而是 windows 对象了。改一下:var d = document;
    var me = this;    //注意这句
       d.onmousedown=function(){
          me.PropertyA = "gg";
          me.FunctionB("usa");
      

  2.   

    function A()
    {
        var me = this;
        this.GetMessages = function()
        {
            me.prototypeA = "haha";
        }
    }
    this.GetMessages 里的实例句柄都用这种 me 的替代模式,变量的作用域用得好,那确实是个好东西
      

  3.   

    不过感谢你让我搞清楚了那个this居然是window对象....狂汗...一直在抓狂..他到底是个啥!!不过还有一个稀奇的问题是这样的..
    //........
    talk.prototype.sendto = "";
    talk.prototype.newmsg = "";
    talk.prototype.http_request = newXMLHttpRequest();
    //......
    //这个东西怪了 下面这个this.http_request对象又可用..
    talk.prototype.GetMessages = function()
    {
        /********************************不能使用this调用基类属性**************************************************************/
        
        if (this.http_request.readyState == 4) 
        { 
        if (this.http_request.status == 200) 
        {
        this.newmsg =  this.http_request.responseText;
       
        if(this.newmsg != this.recive)
                {
                    this.content = this.newmsg;
                    this.recive = this.newmsg;
                    this.talkname = this.sendto;
                    //push in msg list
                    this.Show();
                }
        }
        else 
        { 
        this.newmsg = "Get Data Error!!"; 
        } 
        }
    }
      

  4.   

    这样程序是没问题.不报错.但是和目的不太一样.貌似那个me是一个值传递..me并不能代替this对象来改变this当中的属性值,看能不能把那个me弄成this的引用传递,然后再来修改他就可以了..使用me那样的话,好像只是在那个局部修改me的属性值,并不能在将对象实例化了之后改变倒实例的值.
      

  5.   

    talk.prototype.GetMessages = function()
    这里面的this应该是talk吧?
    刚刚document.onmousedown=function() 里的this是window.
      

  6.   

    <script type="text/javascript">
    function A() {
    var me = this;
    this.G1 = function() {
    this.prototypeA = "haha";
    }
    document.onmousedown = function () {
    alert(me.prototypeA);
    alert(this == window);
    }
    }
    var a = new A();
    a.G1();
    </script>
      

  7.   

    这里的document.onmousedown不是window啊
    meizz(梅花雪) 前辈。。
      

  8.   

    确实是window...问题解决了..感谢meizz(梅花雪) 
    那个me确实作用域是在this里面...虽然很奇怪,先解决问题先..弄完了再来研究...
      

  9.   

    document.onmousedown = function () {
    alert(me.prototypeA);
    alert(this == window);
    }
    这里的this是document测试啦啊。。this == window是false == document是true