javascript this 用法 ... 不明白 !

小范f-li.cn 2008-10-05 10:22:37
加精
function show(){
this.a="1";//我的理解是 这里的 this 指bb
alert(this.a+this.b+this.c+this.d);//显示"1undefinedundefinedundefined"
show2();
function show2(){
this.b="2";
alert(this.a+this.b+this.c+this.d);// 显示 "undefined2undefinedundefined" 为什么 this.a 是 undefined ......
show3();
function show3(){
this.c="3";
alert(this.a+this.b+this.c+this.d);//显示 "undefined23undefined" 这里 为什么 不是 "undefinedundefined3undefined"
show4();
function show4(){
this.d="4";
alert(this.a+this.b+this.c+this.d);//显示 "undefined234" 为什么 this.a 都是 undefined
}
}
}
}
var bb=new show();
我都晕了
最好能 指出 在 show2 里 如何 获取 show 里的 this.a .... - -!
帅哥 靓美 !! 快来啊......
...全文
4830 92 打赏 收藏 转发到动态 举报
写回复
用AI写文章
92 条回复
切换为时间正序
请发表友善的回复…
发表回复
sqcairongbin 2012-04-09
  • 打赏
  • 举报
回复
我去这贴08年的。。
bbjbepzz 2012-03-02
  • 打赏
  • 举报
回复
this.a中的this指向bb對象,其他的this都是指向window對象,唉,又一個被javascript禍害了。
hi_allen_liu 2011-09-14
  • 打赏
  • 举报
回复
学习了
after80 2011-09-02
  • 打赏
  • 举报
回复
我知道了,让楼主迷惑的缘由皆因一个 new !
试试把 var bb=new show(); 改成 var bb= show(); 就会恍然大悟了
dianyancao 2011-05-07
  • 打赏
  • 举报
回复
咪咪Meme-I-love-I
iLove9ouHenry 2011-04-02
  • 打赏
  • 举报
回复
.......
燥动的心 2011-03-21
  • 打赏
  • 举报
回复
这个真是好贴.学习.
frech0better 2011-03-06
  • 打赏
  • 举报
回复
还是有些疑问的。。

一个当前对象,一个window。
new的上下文是当前对象,不new就是window。

show2是show的一个方法,执行一改再当前对像里面啊。。怎么会在window里面呢?

求解。。
beowulf2005 2011-01-13
  • 打赏
  • 举报
回复
楼上的都有没有试一下运行阿
结果明明是
1
12
123
1234

在FF,Chrome下
JKelfin 2011-01-13
  • 打赏
  • 举报
回复
this的话算是个局部的,在函数内部不会被持久化,那么你新建的方法中this会为undefined,
setTimeout也会有同样的问题,
解决的话可以用var pThis = this;在function(setTiemout)里面用pThis这样就不会错误了。
gq198718 2011-01-12
  • 打赏
  • 举报
回复
学习了
magictruth 2010-12-29
  • 打赏
  • 举报
回复
了然,this就是指调用当前函数的对象
jerryxiaosa 2009-12-19
  • 打赏
  • 举报
回复
这是关于this执行上下文的问题
如果当前正在执行的是一个方法,则执行上下文就是该方法所附属的对象,如果当前正在执行的是一个创建对象(就是通过 new 来创建)的过程,则创建的对象就是执行上下文。

function show(){
this.a="1"; //因为var bb=new show(); 所以这里this的执行上下文是bb
alert(this.a+this.b+this.c+this.d);//显示"1undefinedundefinedundefined"
show2();
/*
* 这里没有new操作,也没有显式的声明this的执行上下文,所以this的执行上下文为window
* 可以声明show2.apply(this); 从而获取this.a的值
* show3()、show4()同理
*/
function show2(){
this.b="2";
alert(this.a+this.b+this.c+this.d);// 显示 "undefined2undefinedundefined" 为什么 this.a 是 undefined ......
show3();
function show3(){
this.c="3";
alert(this.a+this.b+this.c+this.d);//显示 "undefined23undefined" 这里 为什么 不是 "undefinedundefined3undefined"
show4();
function show4(){
this.d="4";
alert(this.a+this.b+this.c+this.d);//显示 "undefined234" 为什么 this.a 都是 undefined
}
}
}
}
var bb=new show();
//alert(window.a+wondow.b+window.c+window.d); 试一下这个就知道了
ljjtea 2009-12-03
  • 打赏
  • 举报
回复
有点懂了,但还是有点模糊
renzaijiang 2009-07-19
  • 打赏
  • 举报
回复
我来说句言简意赅的
函数在那个对象里面运行 this就是哪个
new 的就是构造函数
starnight_cbj 2009-06-24
  • 打赏
  • 举报
回复
因为是函数闭包问题,a覆盖了以前的。
http://blog.csdn.net/starnight_cbj/archive/2009/05/26/4216092.aspx
zhiweiok 2009-06-09
  • 打赏
  • 举报
回复
看不懂哇。。。
deqin888 2009-04-21
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript">
function show()
{
this.a="1";
window.a = "1";//这样,后面就能显示this.a了(后三个this实际指向window)
alert(this.a+" "+this.b+" "+this.c+" "+this.d);
if(this.alert)//因为alert实际是window.alert,所以可以用来判断是不是指向window
alert("show的this指向window");
new show2();
function show2()
{
this.b="2";
alert(this.a+" "+this.b+" "+this.c+" "+this.d);
if(this.alert)
alert("show2的this指向window");
new show3();
function show3()
{
this.c="3";
alert(this.a+" "+this.b+" "+this.c+" "+this.d);
if(this.alert)
alert("show3的this指向window");
new show4();
function show4()
{
this.d="4";
alert(this.a+" "+this.b+" "+this.c+" "+this.d);
if(this.alert)
alert("show4的this指向window");
}//end show4
}//end show3
}//end show2
}//end show

var bb=new show();

/*
实例化指向对象,未实例化指向window
*/
</script>
</body>
</html>
blueidea123 2008-10-14
  • 打赏
  • 举报
回复
up
lzp765 2008-10-13
  • 打赏
  • 举报
回复
MARK!
加载更多回复(70)

87,923

社区成员

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

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