JavaScript中this指针浅释
2008-10-23 13:30
295 查看
Page last changed 21 months ago
Work in the Netherlands
About
Resources
Blogs
preferencessitemapcontact
Webontwikkelaars gezocht!
show site navigation
Loading site navigation; please wait...
See sections 5H and 7F of the book.
This page has been translated into French.
One of the most powerful JavaScript keywords is
it is hard to use if you don't exactly know how it works.
Below I explain how to use it in
event handling.
Later on I'll add some information about other uses of
is: What does
In JavaScript
we're executing, or rather, to the object that a function is a method of.
When we define our faithful function
is the page, or rather, the
property, though, is owned by the HTML element it belongs to.
This "ownership" is the result of JavaScript's object oriented approach. See the
Objects as associative arrays page for some more
information.
If we execute
keyword refers to the window and the function tries to change the
window doesn't have a
is "owned" by the correct HTML element. In other words, we have to
copy the function to our onclick property.
Traditional event registration takes care of it.
The function is copied in its entirety to the
a method). So if the event handler is executed
The trick is of course that we can copy the function to several event handlers. Each time
will refer to the correct HTML element:
Thus you use
element that "owns" the copy of
inline event registration
you do not copy the function! Instead, you refer to it, and the difference is crucial.
The
merely a function call:
So it says “Go to doSomething() and execute it.” When
we arrive at
window object and the function returns error messages.
you must make sure that the
Only in that case does it refer to the HTML element the event handler is registered
to. So if you do
you get
As you can see, the
Therefore it refers to the HTML element.
But if you do
you get
This is merely a reference to function
keyword is not present in the
Note the presence of
Microsoft event registration model
is that
does not copy it. Therefore it is sometimes impossible to know which HTML currently handles
the event.
so that you can still use it:
Home
Sitemap
Contact
Copyright
Buy my book
Webontwikkelaars gezocht
Valid XHTML 1.0
Work in the Netherlands
About
Resources
Blogs
preferencessitemapcontact
Webontwikkelaars gezocht!
The this keyword
show page contentsTopOwnerCopyingReferringThe differenceExamples - copyingExamples - referringCombinationshow site navigation
Loading site navigation; please wait...
See sections 5H and 7F of the book.
This page has been translated into French.
One of the most powerful JavaScript keywords is
this. Unfortunately
it is hard to use if you don't exactly know how it works.
Below I explain how to use it in
event handling.
Later on I'll add some information about other uses of
this.
Owner
The question that we'll discuss for the remainder of the pageis: What does
thisrefer to in the function
doSomething()?
function doSomething() { this.style.color = '#cc0000'; }
In JavaScript
thisalways refers to the “owner” of the function
we're executing, or rather, to the object that a function is a method of.
When we define our faithful function
doSomething()in a page, its owner
is the page, or rather, the
windowobject (or global object) of JavaScript. An
onclick
property, though, is owned by the HTML element it belongs to.
This "ownership" is the result of JavaScript's object oriented approach. See the
Objects as associative arrays page for some more
information.
------------ window -------------------------------------- | / / | | | | | this | | ---------------- | | | | HTML element | <-- this ----------------- | | ---------------- | | doSomething() | | | | | ----------------- | | -------------------- | | | onclick property | | | -------------------- | | | ----------------------------------------------------------
If we execute
doSomething()without any more preparation the
this
keyword refers to the window and the function tries to change the
style.colorof the window. Since the
window doesn't have a
styleobject the function fails miserably and produces JavaScript errors.
Copying
So if we want to usethisto its full extent we have to take care that the function that uses it
is "owned" by the correct HTML element. In other words, we have to
copy the function to our onclick property.
Traditional event registration takes care of it.
element.onclick = doSomething;
The function is copied in its entirety to the
onclickproperty (which now becomes
a method). So if the event handler is executed
thisrefers to the HTML element and its
coloris changed.
------------ window -------------------------------------- | | | | | | | ---------------- | | | HTML element | <-- this ----------------- | | ---------------- | | doSomething() | | | | | ----------------- | | ----------------------- | | | |copy of doSomething()| <-- copy function | | ----------------------- | | | ----------------------------------------------------------
The trick is of course that we can copy the function to several event handlers. Each time
this
will refer to the correct HTML element:
------------ window -------------------------------------- | | | | | | | ---------------- | | | HTML element | <-- this ----------------- | | ---------------- | | doSomething() | | | | | ----------------- | | ----------------------- | | | |copy of doSomething()| <-- copy function | | ----------------------- | | | | | | ----------------------- | | | | another HTML element| <-- this | | | ----------------------- | | | | | | | | | ----------------------- | | | |copy of doSomething()| <-- copy function | | ----------------------- | | | ----------------------------------------------------------
Thus you use
thisto the fullest extent. Each time the function is called,
thisrefers to the HTML element that is currently handling the event, the HTML
element that "owns" the copy of
doSomething().
Referring
However, if you useinline event registration
<element onclick="doSomething()">
you do not copy the function! Instead, you refer to it, and the difference is crucial.
The
onclickproperty does not contain the actual function, but
merely a function call:
doSomething();
So it says “Go to doSomething() and execute it.” When
we arrive at
doSomething()the
thiskeyword once again refers to the global
window object and the function returns error messages.
------------ window -------------------------------------- | / / | | | | | this | | ---------------- | | | | HTML element | <-- this ----------------- | | ---------------- | | doSomething() | | | | | ----------------- | | ----------------------- / / | | | go to doSomething() | | | | | and execute it | ---- reference to | | ----------------------- function | | | ----------------------------------------------------------
The difference
If you want to usethisfor accessing the HTML element that is handling the event,
you must make sure that the
thiskeyword is actually written into the
onclickproperty.
Only in that case does it refer to the HTML element the event handler is registered
to. So if you do
element.onclick = doSomething;alert(element.onclick)
you get
function doSomething() { this.style.color = '#cc0000'; }
As you can see, the
thiskeyword is present in the
onclickmethod.
Therefore it refers to the HTML element.
But if you do
<element onclick="doSomething()">alert(element.onclick)
you get
function onclick() { doSomething() }
This is merely a reference to function
doSomething(). The
this
keyword is not present in the
onclickmethod so it doesn't refer to the HTML element.
Examples - copying
thisis written into the
onclickmethod in the following cases:
element.onclick = doSomething element.addEventListener('click',doSomething,false) element.onclick = function () {this.style.color = '#cc0000';} <element onclick="this.style.color = '#cc0000';">
Examples - referring
In the following casesthisrefers to the window:
element.onclick = function () {doSomething()}
element.attachEvent('onclick',doSomething)
<element onclick="doSomething()">
Note the presence of
attachEvent(). The main drawback of the
Microsoft event registration model
is that
attachEvent()creates a reference to the function and
does not copy it. Therefore it is sometimes impossible to know which HTML currently handles
the event.
Combination
When using inline event registration you can also sendthisto the function
so that you can still use it:
<element onclick="doSomething(this)"> function doSomething(obj) { // this is present in the event handler and is sent to the function // obj now refers to the HTML element, so we can do obj.style.color = '#cc0000'; }
Home
Sitemap
Contact
Copyright
Buy my book
Webontwikkelaars gezocht
Valid XHTML 1.0
相关文章推荐
- JavaScript中this指针指向的彻底理解
- JavaScript——对this指针的理解
- 深入认识JavaScript中的this指针
- 谈谈Javascript的this指针
- 详解Javascript 中的this指针
- 深入认识JavaScript中的this指针
- javascript this 指针的使用及常见问题
- 对JavaScript中this指针的新理解分享
- 详解Javascript 中的this指针
- 改变 javascript 函数 内部 this 指针 指向 的三种方法
- javascript对象---7 Function函数 this指针
- javascript中不同方式的嵌套函数中 this指针指向的对象的不同(高手就不用进了,因为我也是菜鸟)
- javascript 的this指针
- Javascript:必须知道的Javascript知识点之“this指针”
- javaScript 的 this指针使用要注意(转)
- javascript 误用this指针 的情况
- JavaScript中的this指针
- 图说js中的this——深入理解javascript中this指针
- 小议Javascript中的this指针
- 【转】JavaScript里的this指针