这种困惑 [英] this confusion

查看:71
本文介绍了这种困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简化的示例代码,可以复制/粘贴并显示

问题。


var ob = {id:2};

ob.alertid = function(){alert(" id =" + this.id);}

ob.funcs = [ob.alertid];

ob.testalert = function(){

this.alertid(); //这个有效

this.funcs [0](); //这不是

}


ob.testalert();


我有一个对象带有id属性。如果直接调用了alertid函数警告

id属性,但是如果通过数组

函数指针调用它则不会。我不明白其中的区别。在这两种情况下,为什么''这''

不同。这是同一个对象方法不是。


输出结果是:


id = 2

id = undefined

Simplified example code which can be copy/pasted and will exhibit the
problem.

var ob={id:2};
ob.alertid=function(){alert("id="+this.id);}
ob.funcs=[ob.alertid];
ob.testalert=function(){
this.alertid(); //this works
this.funcs[0](); //this doesn''t
}

ob.testalert();

I have an object with an id property. The alertid function alerts that
id property if it''s called directly, but not if it''s called via an array
of function pointers. I don''t understand the difference. Why is ''this''
different in those two cases. It''s the same object method isn''t it.

The output is:

id=2
id=undefined

推荐答案

8月9日上午7:23,Stevo< ple ... @ spam-me.comwrote:
On Aug 9, 7:23 am, Stevo <ple...@spam-me.comwrote:

简化的示例代码,可以复制/粘贴并显示

问题。


var ob = {id:2};

ob.alertid = function(){alert(" id =" + this.id);}

ob .funcs = [ob.alertid];

ob.testalert = function(){

this.alertid(); //这个有效

this.funcs [0](); //这不是


}


ob.testalert();


我有一个id属性的对象。如果直接调用了alertid函数警告

id属性,但是如果通过数组

函数指针调用它则不会。我不明白其中的区别。在这两种情况下,为什么''这''

不同。这是同一个对象方法不是。
Simplified example code which can be copy/pasted and will exhibit the
problem.

var ob={id:2};
ob.alertid=function(){alert("id="+this.id);}
ob.funcs=[ob.alertid];
ob.testalert=function(){
this.alertid(); //this works
this.funcs[0](); //this doesn''t

}

ob.testalert();

I have an object with an id property. The alertid function alerts that
id property if it''s called directly, but not if it''s called via an array
of function pointers. I don''t understand the difference. Why is ''this''
different in those two cases. It''s the same object method isn''t it.



我认为关于这个关键字的最好的帖子就是这里的一个

by Mike Winter:


< URL:
http://groups.google.com.au/group/co ... 26cd885e01c292


>
>



第一部分是闭包,第二部分是

优秀。

-

Rob


The first part is about closures, the second part on this is
excellent.
--
Rob


8月8日下午5:23,Stevo< ple ... @ spam-me.comwrote:
On Aug 8, 5:23 pm, Stevo <ple...@spam-me.comwrote:

简化的示例代码,可以复制/粘贴并显示

问题。


var ob = {id:2};

ob.alertid = function(){alert(" id =" + this.id);}

ob .funcs = [ob.alertid];

ob.testalert = function(){

this.alertid(); //这有效
Simplified example code which can be copy/pasted and will exhibit the
problem.

var ob={id:2};
ob.alertid=function(){alert("id="+this.id);}
ob.funcs=[ob.alertid];
ob.testalert=function(){
this.alertid(); //this works



对。因为你调用了ob对象的方法。在这种情况下,

this是ob,它有一个id属性。

Right. Because you called a method of the ob object. In this case,
"this" is ob, which has an id property.


this.funcs [0](); //这不是
this.funcs[0](); //this doesn''t



funcs [0]包含对匿名函数的引用。 ob

有一个属性(alertid方法)引用它的事实是

无意义。在这种情况下,这个是指是指匿名函数,

没有id属性。

funcs[0] holds a reference to an anonymous function. The fact that ob
has a property (alertid method) that references it as well is
meaningless. In this case, "this" refers to the anonymous function,
which has no id property.


David Mark写道:
David Mark wrote:

在这种情况下,this是指匿名函数,

没有id属性。
In this case, "this" refers to the anonymous function,
which has no id property.



感谢David,Randy,Thomas和Rob。你们都说了同样的话

基本上,这取决于它的调用方式。优秀链接

Rob,这是一个书签。


我已经改变了alertid函数来添加''那个'现在它工作。


var ob = {id:2};

ob.alertid = function(){var that = ob; alert(" id = " + that.id);}

ob.funcs = [ob.alertid];

ob.testalert = function(){

this.alertid();

this.funcs [0]();

}

ob.testalert();


虽然在alertid函数中具有ob硬编码的名称。我想知道

如果可以在不知道

对象名称的情况下创建''那个'?用以下示例???我不知道该设置什么:


var ob_a = {id:" a"};

var ob_b = {id:" ; b"};

var alertid = function(){var that = ???; alert(" id =" + that.id);}

ob_a.alertid = alertid;

ob_b.alertid = alertid;


我猜是没有办法定义''那个' '这对于两个对象都可以用于

,并且我必须为每个对象定义一个匿名函数

,其中包含对ob_a和ob_b的硬编码引用他们是这样的:


var ob_a = {id:" a"};

var ob_b = {id:" b"};

ob_a.alertid = function(){var that = ob_a; alert(" id =" + that.id);};

ob_b.alertid = function() {var that = ob_b; alert(" id =" + that.id);}

Thanks David, Randy, Thomas and Rob. You all said the same thing
basically, this differs depending on how it''s called. Excellent link
Rob, that''s one to bookmark.

I''ve altered the alertid function to add ''that'' and it now works.

var ob={id:2};
ob.alertid=function(){var that=ob;alert("id="+that.id);}
ob.funcs=[ob.alertid];
ob.testalert=function(){
this.alertid();
this.funcs[0]();
}
ob.testalert();

That has the name ob hard-coded in the alertid function though. I wonder
if it''s possible to create ''that'' without knowing the name of the
object? Take the following example with ??? where I don''t know what to set:

var ob_a={id:"a"};
var ob_b={id:"b"};
var alertid=function(){var that=???;alert("id="+that.id);}
ob_a.alertid=alertid;
ob_b.alertid=alertid;

I''m guessing that there''s no way to define ''that'' which would work for
both objects there, and I''d have to define an anonymous function for
each object, with hard-coded references to ob_a and ob_b in them like so:

var ob_a={id:"a"};
var ob_b={id:"b"};
ob_a.alertid=function(){var that=ob_a;alert("id="+that.id);};
ob_b.alertid=function(){var that=ob_b;alert("id="+that.id);}


这篇关于这种困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆