晕...居然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()这个方法当中获取或设置这个类的属性..谢谢.分不够再加
继续..
在调用的时候则出现了问题:在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()这个方法当中获取或设置这个类的属性..谢谢.分不够再加
this.PropertyA = "gg";
你这样定义的话,这里面的 this 已经不是 A 而是 windows 对象了。改一下:var d = document;
var me = this; //注意这句
d.onmousedown=function(){
me.PropertyA = "gg";
me.FunctionB("usa");
{
var me = this;
this.GetMessages = function()
{
me.prototypeA = "haha";
}
}
this.GetMessages 里的实例句柄都用这种 me 的替代模式,变量的作用域用得好,那确实是个好东西
//........
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!!";
}
}
}
这里面的this应该是talk吧?
刚刚document.onmousedown=function() 里的this是window.
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>
meizz(梅花雪) 前辈。。
那个me确实作用域是在this里面...虽然很奇怪,先解决问题先..弄完了再来研究...
alert(me.prototypeA);
alert(this == window);
}
这里的this是document测试啦啊。。this == window是false == document是true