Two things to say: A probe into the use of 1.this pointers. 2.ie Attachevent and Firefox addeventlistener differences in event handling _javascript skills

Source: Internet
Author: User
First thing.
This clock is a special finger clock in the JavaScript language that points to the current object that calls the this statement when the code is running.
If it is an event-bound function, it points to the bound element itself.
<script type= "Text/javascript" >
by Go_rush (Asun) from http://ashun.cnblogs.com/
Alert (This===window)//true Straight
When the call is received, point to window itself
var gorush={
F:function () {
Alert (This===gorush)//true
}
}
GORUSH.F ()//point to Gorush Object
Document.onclick=function () {
Alert (this===document)//true, pointing to document
}
/*
Element.onclick=function () {
Alert (this===element)//true
}
*/
</script>
Notably, when multiple objects are nested, this refers to the object that has recently called it.
obj1={
obj2:{
F:function () {
Alert (THIS===OBJ1.OBJ2)//Here This does not point to obj1 oh.
}
}
}
OBJ1.OBJ2.F ()
To give a very error-prone example, click here to see the relevant links
<script type= "Text/javascript" >
by Go_rush from http://ashun.cnblogs.com/
The use of this in the following gorush1 is wrong, this error 10 programmers 6 commit
var gorush1={
Showmsg:function () {alert ("Hello,world")},
Doajax:function () {
New Ajax.request ("index.php", {onsuccess:function () {
This.showmsg ()
}})
}
}
Gorush2 is right.
var gorush2={
Showmsg:function () {alert ("Hello,world")},
Doajax:function () {
var self=this; Backing up GORUSH2 objects
New Ajax.request ("index.php", {onsuccess:function () {
Self.showmsg ()
}})
}
}
</script>

Second thing:
Gossip is not much to say, first dish dishes.
<script type= "Text/javascript" >
var btn=null
Window.onload=function () {
Btn=document.getelementbyid ("BTN")
if (window.attachevent) btn.attachevent ("onclick", Gorush);
if (Window.addeventlistener) Btn.addeventlistener ("click", Gorush,false)
}
function Gorush () {
if (This===window) alert ("This==window")//ie6.0, this sentence will execute
if (THIS===BTN) alert ("This==btn")//ff1.5, this sentence will execute
}
</script>
<input type= "button" value= "click Me" id= "btn" >
I really do not understand why IE will do so, people are very depressed ah, why this point to the window?
Workaround:
1. When the event is bound don't use attachevent, poor me, that was the Prototype.js Event.observe method
So element.onclick=function ... So in two browsers this means the clock points to element
2. Use the GetEvent () method to get the event uniformly in the processing function Gorush, and then use Evt.srcelement | | Evt.target Get the Element object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.