`
boin
  • 浏览: 34129 次
社区版块
存档分类
最新评论

The this Keyword in JavaScript

阅读更多
JavaScript this 关键字

翻译自PPK Book

this 是 JavaScript 中很强大的关键字之一。但是不幸的是,如果你不能完全明白它的特征和用法的话,你可能会在使用的时候碰到很多问题。

本文从事件响应的列子开始,来试图说明this关键字的用法和一些知识。

Owner(宿主)

在余下的篇幅中,我们会一直围绕着一个函数来讨论,doSomething(),在这个方法中,this到底指向什么?
function doSomething() {
   this.style.color = '#cc0000';
}

在JavaScript中this是始终指向正在被执行的方法的“owner”(宿主),或者是包含这个方法的对象本身。如果我们在某个页面中定义了一个doSomething()函数,那么这个方法的owner则是这个页面,即当前window对象或JavaScript的全局对象。举个例子来说:一个“onclick”属性是被包含于某个HTML元素中的,那这个属性的owner就是这个HTML元素。

ownership(存在关系)是JavaScript的面向对象模型的产物 。实际上对象都是以关联数组的形式来组织的。

------------ window --------------------------------------
|                                          / \           |
|                                           |            |
|                                          this          |
|   ----------------                        |            |
|   | HTML element | <-- this         -----------------  |
|   ----------------      |           | doSomething() |  |
|               |         |           -----------------  |
|          --------------------                          |
|          | onclick property |                          |
|          --------------------                          |
|                                                        |
----------------------------------------------------------


当我们不做任何准备直接执行doSomething()函数的话,此时this是指向window对象的,然后方法试图改变window对象的style.color,当然,此时window对象并不存在这样一个style对象,所以该函数执行失败,并抛出一个JavaScript错误。

Copying(拷贝)

如果我们想要这个函数正常工作的话,我们就要让这个函数被包含在正确的HTML对象中。
换句话来说,就是我们要把这个函数“拷贝”到onclick属性上。然后传统的事件注册模型会自动处理余下的工作。
element.onclick = doSomething;

这段代码把整个doSomething()函数拷贝到了某个HTML元素的onclick属性上(然后这个函数变成了一个成员方法)这时再单击该HTML元素时,事件响应会自动调用onclick属性绑定的方法,此时this已经指向了该HTML对象,然后方法顺利执行,改变了element元素的原色。
------------ window --------------------------------------
|                                                        |
|                                                        |
|                                                        |
|   ----------------                                     |
|   | HTML element | <-- this         -----------------  |
|   ----------------      |           | doSomething() |  |
|               |         |           -----------------  |
|          -----------------------          |            |
|          |copy of doSomething()|  <-- copy function    |
|          -----------------------                       |
|                                                        |
----------------------------------------------------------

我们可以把这个函数拷贝到很多个HTML对象的事件响应函数中。然后每次运行的时候this就会指向正确的HTML对象。
------------ window --------------------------------------
|                                                        |
|                                                        |
|                                                        |
|   ----------------                                     |
|   | HTML element | <-- this         -----------------  |
|   ----------------      |           | doSomething() |  |
|               |         |           -----------------  |
|          -----------------------          |            |
|          |copy of doSomething()|  <-- copy function    |
|          -----------------------          |            |
|                                           |            |
|   -----------------------                 |            |
|   | another HTML element| <-- this        |            |
|   -----------------------     |           |            |
|               |               |           |            |
|          -----------------------          |            |
|          |copy of doSomething()|  <-- copy function    |
|          -----------------------                       |
|                                                        |
----------------------------------------------------------

做完所有拷贝后,当函数被执行时,this就被自动指向到正确的响应这个事件的HTML对象上了,此时该HTML对象就包含了doSomething()这个函数的拷贝。

Referring(引用)
你还可以使用inline(内联)的方式来绑定这个函数,即在HTML文档中对HTML元素制定onclick属性。
<element onclick="doSomething()">

记住,此时你并没有拷贝这个函数!你只是指向了这个函数,这个差异是很不容忽视的。这时onclick属性并没有包含这个方法,只是包含了对这个方法的一个调用。
doSomething();

此时当元素被单击时,代码的动作是“找到doSomething()方法,然后执行”。然后当我们找到并执行doSomething()方法时,this又被指向到了全局window对象,函数就会产生错误。
------------ window --------------------------------------
|                                          / \           |
|                                           |            |
|                                          this          |
|   ----------------                        |            |
|   | HTML element | <-- this         -----------------  |
|   ----------------      |           | doSomething() |  |
|               |         |           -----------------  |
|          -----------------------         / \           |
|          | go to doSomething() |          |            |
|          | and execute it      | ---- reference to     |
|          -----------------------       function        |
|                                                        |
----------------------------------------------------------

The difference(区别)

如果你要在事件响应中用this来访问HTML元素的话,你就必须保证this关键字已经写入到了onclick属性中。只有这样才能让this正确的指向HTML对象。所以你执行以下代码时:
element.onclick = doSomething;
alert(element.onclick)

你会得到
function doSomething()
{
	this.style.color = '#cc0000';
}

就像alert的结果所展示的,this关键字已经存在于onclick方法中了,于是它就指向到了该HTML元素。

如果这样做:
<element onclick="doSomething()">
alert(element.onclick)

你会得到
function onclick()
{
	doSomething()
}

这只是一个包含doSomething()函数的调用,this关键字并不存在于onclick方法中,当然也不会指向HTML对象。

示例 - copying

下列情形中,this已经被写入了onclick方法:
element.onclick = doSomething
element.addEventListener('click',doSomething,false)
element.onclick = function () {this.style.color = '#cc0000';}
<element onclick="this.style.color = '#cc0000';">

示例 - referring
下列情形中,this指向的是window对象:
element.onclick = function () {doSomething()}
element.attachEvent('onclick',doSomething)
<element onclick="doSomething()">

注意attachEvent()这个方法。微软事件注册模型的主要缺点是attachEvent()只是创建一个函数的引用,而并没有拷贝函数。因此,有些时候它是完全不知道目前的事件是绑定在哪个HTML对象的。

Combination(结合)

当你使用内联方式注册事件时,你可以指定this引用作为参数传递给响应方法。
<element onclick="doSomething(this)">
function doSomething(obj) {
	// this是存在于这个方法中的,只不过名字变成了obj
	// obj此时就指向了正确的HTML对象,然后我们就可以继续操作了
	obj.style.color = '#cc0000';
}
分享到:
评论

相关推荐

    JavaScript this keyword

    Study note on htis keyword in JavaScript

    Learn by Example: JavaScript for Front-End and Mobile App Development

    This book aims to take someone completely new...The this keyword Get elements from the DOM Building your first mobile app Becoming an app developer Deploying your app to the Android and iTunes app stores

    ssd5 ex4 给需要帮助的人。这是ssd5 ex4

    This method returns a copy of the invoking Listing object sorted by the field name given in the parameter. Use an appropriate STL sorting function to sort the advertisements. To sort based on the ...

    Pro JavaScript Techniques (2nd)

    You will learn about the 'this' keyword, as well as new object tools. You will be able to create reusable code with encapsulation, overloading and inheritance. The most recent techniques for debugging...

    ABAP751 ABAP - Keyword Documentation

    This documentation describes the syntax and meaning of the keywords of the ABAP language and its object-oriented part ABAP Objects. Alongside this, language frameworks and the associated system ...

    Pro JavaScript Techniques(Apress,2ed,2015)

    You will learn about the 'this' keyword, as well as new object tools. You will be able to create reusable code with encapsulation, overloading and inheritance. The most recent techniques for debugging...

    KEYWORD.pdf

    This documentation and the software described in this documentation are subject to change without prior notice No part of this documentation may be reproduced or distributcd in any form without prior...

    Concurrency In Go: Tools And Techniques For Developers PDF

    I’m delighted that you’ve picked up this book and excited to join you in exploring the topic of concurrency in Go over the next six chapters! Go is a wonderful language. When it was first announced...

    the-keyword-this

    ##目标通过完成一系列活动来更好地理解Javascript中的this关键字 ## Directions分叉此仓库,克隆您的分叉,然后打开Practice.js并完成所有活动。 会费 如果您发现问题或错别字,请进行分叉,进行必要的更改,然后...

    Interactive.Object.Oriented.Programming.in.Java

    Discover object oriented programming with Java in this unique tutorial. This book uses Java and Eclipse to write and generate output for examples in topics such as classes, interfaces, overloading, ...

    Solr in Action

    Solr in Action is the definitive guide to implementing fast and scalable search using Apache Solr. It uses well-documented examples ranging from basic keyword searching to scaling a system for ...

    Frontiers in massive Data Analysis

    requiring sophisticated analysis techniques that go far beyond classical indexing and keyword counting, aiming to find relational and semantic interpretations of the phenomena underlying the data....

    微软内部资料-SQL性能优化5

    If you build a clustered index without specifying the unique keyword, SQL Server forces uniqueness by adding a uniqueifier to the rows when necessary. This uniqueifier is a 4-byte value added as an ...

    Search Engine Optimization All-in-One For Dummies(3ed,2015)

    In minibooks that cover the entire topic, you’ll discover how search engines work, how to apply effective keyword strategies, ways to use SEO to position yourself competitively, the latest on ...

    Programming in Objective-C 4th Edition

    The copy and mutableCopy Methods 413 Shallow Versus Deep Copying 416 Implementing the &lt;NSCopying&gt; Protocol 418 Copying Objects in Setter and Getter Methods Exercises 423 19 Archiving 425 Archiving ...

    Search Engine Marketing, Inc.: Driving Search Traffic to Your Company’s Web Site

    including the sea change in paid search toward hybrid auctions, the advent of sitemaps to index your organic content, new keyword tools, and more. In addition, two brand-new chapters have been added:...

    Programming Ruby 1.9 & 2.0 The Pragmatic Programmers’ Guide 4th Edition

    The major language changes in Ruby 2.0 are the addition of keyword arguments and the change to use UTF-8 as the default source file encoding. There are a number of additions to the standard library, ...

    Spotting Outliers in Large Distributed Datasets using

    In this work we propose cell density based mechanism for outlier detection (CDOD) in large distributed databases. A centralized detection paradigm is used; it allows overcoming the expensive data ...

    Relevance Ranking

    (IR) style keyword search on the web, keyword search on XML has emerged recently. The difference between text database and XML database results in three new challenges: (1) Identify the user search ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    More complex inline functions may also be put in a .h file for the convenience of the implementer and callers, though if this makes the .h file too unwieldy you can instead put that code in a ...

Global site tag (gtag.js) - Google Analytics