JS Strict mode under this point to __js

Source: Internet
Author: User
1. This in the global scope

In strict mode, in the global scope, this points to the Window object

  "Use strict";
    
    Console.log ("strict mode");
    Console.log ("This in global scope");
    Console.log ("this.document = = Document", this.document = = document);
    Console.log ("This = = Window", this = = window);
    THIS.A = 9804;
    Console.log (' THIS.A = = window.a=== ', WINDOW.A)
    
    Console.log ("strict mode");
    Console.log ("This in global scope");
    Console.log ("this.document = = Document", this.document = = document);
    Console.log ("This = = Window", this = = window);
    THIS.A = 9804;
    Console.log (' THIS.A = = window.a=== ', WINDOW.A);

2. This in a function in the global scope

In strict mode, this is equal to undefined in this function

"Use strict";
    
    Console.log ("strict mode");
    Console.log (' This ' in a function in the global scope);
    Function F1 () {
      console.log (this);
    }
    
    function F2 () {
      function f3 () {
        console.log (this);
      }
      F3 ();
    }
    F1 ();
    F2 ();

3. This in the function (method) of the object

In strict mode, this in the function of an object points to an instance of an object calling a function

  "Use strict";
    
    Console.log ("strict mode");
    Console.log ("This" in a function of an object);
    var o = new Object ();
    O.A = ' o.a ';
    O.f5 = function () {return
      this.a;
    }
    Console.log (O.f5 ());

4. This of the constructor function

In strict mode, this in the constructor points to the object instance created by the constructor.

  "Use strict";
    
    Console.log ("strict mode");
    Console.log ("This" in the constructor);
    function Constru () {
      this.a = ' constru.a ';
      THIS.F2 = function () {
        console.log (this.b);
        Return THIS.A
      }
    }
    var O2 = new Constru ();
    o2.b = ' o2.b ';
    Console.log (O2.f2 ());

5. This in the event handler function

In strict mode, in the event handler function, this points to the target object that triggered the event.

    "Use strict";    
  function Blue_it (e) {
      if (this = = E.target) {
        this.style.backgroundColor = ' #00f ';
      }
    }
    var elements = document.getelementsbytagname (' * ');
    for (var i=0; I<elements.length i++) {
      Elements[i].onclick = blue_it;
    }
    
    The purpose of this code is to make the clicked element background color blue
6. This in the inline event handler function

In strict mode, in the inline event handler, there are two things:

   <button onclick= "Alert ((function () {' Use strict '; return this}) ());" >
      inline Event handling 1
    </button>
    <!--warning window characters for undefined-->
    
    <button onclick= "' Use Strict '; Alert (This.tagName.toLowerCase ()); " >
      Inline Event handling 2
    </button>
    <!--the characters in the Warning window are button-->

Resources

MDN https://developer.mozilla.org ...

Extension information

Nanyi Web log > JavaScript Strict mode detailed Http://www.ruanyifeng.com/blo ...

Rookie Tutorials > JavaScript Strict mode http://www.runoob.com/js/js-s ...


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.