在面向对象编程语言中,对于this关键字我们是非常熟悉的。比如C++、C#和Java等都提供了这个关键字,虽然在开始学习的时候觉得比较难,但只要理解了,用起来是非常方便和意义确定的。JavaScript也提供了这个this关键字,不过用起来就比经典OO语言中要"混乱"的多了。
下面就来看看,在JavaScript中各种this的使用方法有什么混乱之处?
1、在HTML元素事件属性中inline方式使用this关键字:
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
我们一般比较常用的方法是在此使用:javascirpt: EventHandler(this),这样的形式。不过这里其实可以写任何合法的JavaScript语句,要是高兴在此定义个类也可以(不过将会是个内部类)。这里的原理是脚本引擎生成了一个div实例对象的匿名成员方法,而onclick指向这个方法。
2、用DOM方式在事件处理函数中使用this关键字:
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://birdshome.cnblogs.com/Images/dot.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://birdshome.cnblogs.com/Images/dot.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这时的EventHandler()方法中的this关键字,指示的对象是IE的window对象。这是因为EventHandler只是一个普通的函数,对于attachEvent后,脚本引擎对它的调用和div对象本身没有任何的关系。同时你可以再看看EventHandler的caller属性,它是等于null的。如果我们要在这个方法中获得div对象引用,应该使用:this.event.srcElement。
3、用DHTML方式在事件处理函数中使用this关键字:
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://birdshome.cnblogs.com/Images/dot.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://birdshome.cnblogs.com/Images/dot.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
这里的this关键字指示的内容是div元素对象实例,在脚本中使用DHTML方式直接为div.onclick赋值一个EventHandler的方法,等于为div对象实例添加一个成员方法。这种方式和第一种方法的区别是,第一种方法是使用HTML方式,而这里是DHTML方式,后者脚本解析引擎不会再生成匿名方法。
4、类定义中使用this关键字:
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
这是JavaScript模拟类定义中对this的使用,这个和其它的OO语言中的情况非常的相识。但是这里要求成员属性和方法必须使用this关键字来引用,运行上面的程序会被告知myName未定义。
5、为脚本引擎内部对象添加原形方法中的this关键字:
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
这里的this指代的是被添加原形的类的实例,和4中类定义有些相似,没有什么太特别的地方。
6、结合2&4,说一个比较迷惑的this关键字使用:
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
我就说说结果,页面运行后会显示:"division element",确定后点击文字"division element",将会显示:"undefined"。
7、CSS的expression表达式中使用this关键字:
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
height: expression(this.parentElement.height);" >
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
这里的this看作和1中的一样就可以了,它也是指代div元素对象实例本身。
8、函数中的内部函数中使用this关键字:
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://birdshome.cnblogs.com/Images/OutliningIndicators/None.gif)