This keyword usage in Javascript

Source: Internet
Author: User

In JavascriptthisAlways refers to the "owner" of the function we're re executing, or
Rather, to the object that a function is a method.

function doSomething() {   this.style.color = '#cc0000';}

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

1. Copy---------------Traditional event registration--------------the element
element.onclick = doSomething;

The function is copied in its entirety toonclickProperty (which now becomes a method). So if the event handler is executedthisRefers
To the HTML Element and ItscolorIs changed.

------------ window --------------------------------------|                                                        ||                                                        ||                                                        ||   ----------------                                     ||   | HTML element | <-- this         -----------------  ||   ----------------      |           | doSomething() |  ||               |         |           -----------------  ||          -----------------------          |            ||          |copy of doSomething()|  <-- copy function    ||          -----------------------                       ||                                                        |----------------------------------------------------------

Examples of elements:

element.onclick = doSomethingelement.addEventListener('click',doSomething,false)element.onclick = function () {this.style.color = '#cc0000';}<element onclick="this.style.color = '#cc0000';">

2. Reference ------------- inline
Event registration ------------ this points to the global variable window

doSomething();

So it says "Go to dosomething () and execute it." When we arrivedoSomething()ThethisKeyword once again refers
To the global window object and the function returns error messages.

This indicates an example of a window object.

element.onclick = function () {doSomething()}element.attachEvent('onclick',doSomething)<element onclick="doSomething()">

From: http://www.quirksmode.org/js/this.html

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.