"JavaScript notes" this in JavaScript

Source: Internet
Author: User

The previous understanding of this is that you probably know what it is. Recently saw the introduction above the MDN, just record it.
MDN official English Explanation: The This iskeyword refers to the function ' s execution context.

Global context

  • The global context can be understood as all this object outside the [object Function] and [Object Object] type.
    This is the global Window object under Chrome Devtool.
    This is the global globally object under the node environment
    //chrome F12      window===this  //true    //node     global===this //true

function Context
The This in the function I understand is who calls this function, and this is the object that refers to the function called.

    (function(){console.log(this==global)})()  //node环境下为true (function(){console.log(this==window)})() //浏览器环境下为true

但是构造函数有点特殊,当一个函数被作为一个构造函数来使用(使用new关键字),它的this与即将被创建的新对象绑定。

For reference, click: _this in https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/this# constructor

"JavaScript notes" this in 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.