Why “this” inside of a function's returning object window

后端 未结 3 1555
萌比男神i
萌比男神i 2021-01-18 18:14

there is two type of scope in javascript named function scope global scope

now i am executing this code

function abc()
{
alert(this)         


        
3条回答
  •  一个人的身影
    2021-01-18 18:34

    this, inside any function, will be the object on which the function is invoked. In your case, you are not invoking it on any object. So, by default this refer to global object, in your browser, it is the window object.

    But in strict mode, if you invoke it like this, this will be undefined.

    "use strict";
    function abc() {
        console.log(this);    // undefined
    }
    abc();
    

    Or

    function abc() {
        "use strict";
        console.log(this);   // undefined
    }
    abc();
    

提交回复
热议问题