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

后端 未结 3 1518
萌比男神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:31

    Your function is under global(window) object. I mean,

    function abc()
    {
        alert(this);
    }
    abc(); 
    // or You can either call by
    window.abc()
    

    You can write your function under custom object

    // Function under custom object
    var customObj = {
        abc : function () {
            alert(this);
        }
    };
    customObj.abc()
    

提交回复
热议问题