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