A few examples of this mechanism inside JavaScript

Source: Internet
Author: User

The This value in JavaScript changes depending on the use of the scene, but there is always a principle that this will always point to the object that is currently calling the function. I'll give you a few examples to illustrate the problem.
The 1.this itself always points to an instance of the current class
function ShowMsg () {
var msg=1;
alert (this.msg);
}
ShowMsg ();
This is called when the undefined is displayed, because ShowMsg has no instance, so it cannot be used
2. Let's look at a click event
<div id= "nav" onclick= "getId ();" >ddd</div>
function GetId () {
Alert (this.id)
}

The result of the run in the segment code shows that this.id is undefined, judging who the this is pointing to, and when it is not defined at execution time. As long as the function is not bound to use on the object, then this is the window. Inside this is the inside of the GetID function, but not the div inside the DOM.
This code is equivalent to Div.onclick = function () {getId ();}
There are several workarounds
A.
<div id= "NAV" >ddd</div>
Window.onload=initform;
function initform ()
{
document.getElementById ("Nav"). Onclick=getid;
}
function GetId () {
Console.log (This)
Alert (this.id)
}

In this code, the function is bound to the object. So the result is feasible.
B.
<div id= "nav" onclick= "getId (this);" >ddd</div>
function GetId (elem)
{
alert (elem.id);
}

This code is equivalent to passing this to Elem, Elem.id is available.
3. For the second example of the C method, I would like to say something more
<div id= "nav" onclick= "getId (this);" Xid= "Weixin" >ddd</div>
function GetId (elem)
{
Console.log (Elem)
alert (elem.id); This is a possible ID is an attribute in HTML
alert (ELEM.XID); But this is not possible because XID is not an intrinsic property of $ (item). attr ("XID")
Alert (this.id)//This is not possible
}

4. You can look at this example again, just like the previous one.

<form action= "#" >
<select id= "NAV" >
<option value= "" >Month</option>
<option value= "0" >January</option>
<option value= "1" >February</option>
<option value= "2" >March</option>
</select>
</form>

Window.onload=initform;
function initform () {
document.getElementById ("Nav"). onchange = GetId;
}
function GetId () {
Console.log (This)
var xid= this.options[this.selectedindex].value;
alert (XID);
}//This can get the ID value

This is also possible to get results.

A few examples of this mechanism inside JavaScript

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.