javascript this详解(转) --http://www.cnblogs.com/felix2007/archive/2007/03/21/682670.html
Posted on 2009-03-10 18:29 懒人ABC 阅读(204) 评论(0) 编辑 收藏 举报下面就来看看,在JavaScript中各种this的使用方法有什么混乱之处?
1、在HTML元素事件属性中inline方式使用this关键字:
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
我们一般比较常用的方法是在此使用:javascirpt: EventHandler(this),这样的形式。不过这里其实可以写任何合法的JavaScript语句,要是高兴在此定义个类也可以(不过将会是个内部类)。这里的原理是脚本引擎生成了一个div实例对象的匿名成员方法,而onclick指向这个方法。
2、用DOM方式在事件处理函数中使用this关键字:
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这时的EventHandler()方法中的this关键字,指示的对象是IE的window对象。这是因为EventHandler只是一个普通的函数,对于attachEvent后,脚本引擎对它的调用和div对象本身没有任何的关系。同时你可以再看看EventHandler的caller属性,它是等于null的。如果我们要在这个方法中获得div对象引用,应该使用:this.event.srcElement。
3、用DHTML方式在事件处理函数中使用this关键字:
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
这里的this关键字指示的内容是div元素对象实例,在脚本中使用DHTML方式直接为div.onclick赋值一个EventHandler的方法,等于为div对象实例添加一个成员方法。这种方式和第一种方法的区别是,第一种方法是使用HTML方式,而这里是DHTML方式,后者脚本解析引擎不会再生成匿名方法。
4、类定义中使用this关键字:
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
这是JavaScript模拟类定义中对this的使用,这个和其它的OO语言中的情况非常的相识。但是这里要求成员属性和方法必须使用this关键字来引用,运行上面的程序会被告知myName未定义。
5、为脚本引擎内部对象添加原形方法中的this关键字:
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
这里的this指代的是被添加原形的类的实例,和4中类定义有些相似,没有什么太特别的地方。
6、结合2&4,说一个比较迷惑的this关键字使用:
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
我就说说结果,页面运行后会显示:"division element",确定后点击文字"division element",将会显示:"undefined"。
7、CSS的expression表达式中使用this关键字:
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
height: expression(this.parentElement.height);">
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
这里的this看作和1中的一样就可以了,它也是指代div元素对象实例本身。
8、函数中的内部函数中使用this关键字:
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
运行结果显示是:"Inner Name, Outer Name"。按我们在2中的讲解,这里的结果如果是"Inner Name, undefined"似乎更合理些吧?但是正确的结果确实是前者,这是由于JavaScript变量作用域的问题决定的,详细了解推荐参看"原来JScript中的关键字'var'还是有文章的"一文及回复。