The This keyword in JavaScript is detailed

Source: Internet
Author: User

What is this in 1.javascript?
This refers to the principal that the current behavior is executing, or the principal that the current method executes
Context: The environment in which the current behavior or method is executed
Instance:
XX go to Beijing Hotel to eat; the context is "Beijing hotel", this is XX

2. So how can you tell who the This keyword is in the body of a function when it executes? There are several main rules:

1) The This keyword in a function body and where the function is defined, where execution is not related;

2) When judging the execution of a method, who is the executing body of the function? The main thing to see is that there is no point (.) in front of the method, and if the function is executed, the function is preceded by a bit (.), and the point (.) is preceded by WHO, when the function is executed, who is the This keyword in the function body;

And look at the code:

When the function executes, there is no point in front of the case:

function fn () {    Console.log (this= 'Windwo ";
FN ();

When the function is executed in a xx.fn manner:

person = {   name:' Alice ',}function  fn () {    Console.log (this  . name);}
= Fn;person.fn ();

3) The This keyword in the function body is always the Window object (and where the self-executing function executes without any relationship), since execution of the function is performed;

4) An event that is bound to an element, when the event is triggered, is bound to an event handler on the element being executed, and the This keyword of the function points to the element of the binding event;

      

  

The This keyword in JavaScript is detailed

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.