javascript中this的用法

infaye 2008-07-22 03:50:44
感觉太灵活,不知道能有什么办法能准确识别this指向的是哪个对象?
...全文
533 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
helei123a 2008-07-22
  • 打赏
  • 举报
回复
指向他自己
bhtfg538 2008-07-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ncm0227120 的回复:]
this 一般指的是当前的对象。也就是你当前所操作的对象,方法...
[/Quote]
正解
infaye 2008-07-22
  • 打赏
  • 举报
回复
var self = this;
timer=windows.setInterval(self.drop.bind(self),50);
和以下代码的this是同样的吗?
timer=windows.setInterval(this.drop.bind(this),50);
阿云ivan 2008-07-22
  • 打赏
  • 举报
回复
<input id="btn1" type="button" value="Button1" onclick="javascript:alert(this.id);" />
<script language="javascript" type="text/javascript">
function fun(){
this.open("*.aspx", "null", "");
}
</script>
<input id="btn2" type="button" value="Button2" onclick="javascript:fun();" />

第一个this指的是按钮btn1对象
第二个this指的是窗体window对象
infaye 2008-07-22
  • 打赏
  • 举报
回复
var self = this;
timer=windows.setInterval(self.drop.bind(self),50);
和以下代码的this是同样的吗?
timer=windows.setInterval(this.drop.bind(this),50);
ncm0227120 2008-07-22
  • 打赏
  • 举报
回复
<marquee direction="up" onmousemove="this.stop()" onmouseout="this.start()" scrollamount="1">淡淡的</marquee>
这是一个滚动的文字 这里的this==当你用鼠标移动到(淡淡的)这个东东
zhangshenqiu 2008-07-22
  • 打赏
  • 举报
回复
当前对象吧。去百度搜搜就解释得很清楚了。
Fus371x 2008-07-22
  • 打赏
  • 举报
回复
关键字this总是指向调用该方法的对象.
可以用alert(this.tostring())或者alert(this.valueof())测试调用的对象的名字
ncm0227120 2008-07-22
  • 打赏
  • 举报
回复
this 一般指的是当前的对象。也就是你当前所操作的对象,方法...
infaye 2008-07-22
  • 打赏
  • 举报
回复
我是个菜鸟,不是很懂楼上的代码的含义,bhtfg538 能否解释一下!谢谢!
bhtfg538 2008-07-22
  • 打赏
  • 举报
回复
alert(this);
function()
{
var me=this;
alert(me);
this.name=function(){alert(me)}
}

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧