鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ js 函數(shù)相互調(diào)用時(shí),this 指向

js 函數(shù)相互調(diào)用時(shí),this 指向

在 hello() 中調(diào)用 world() ,為什么 world() 中的 this 指向的是 window?
是不是因?yàn)樵谡{(diào)用world()時(shí),沒有明確的使用對象或 this,所以 world() 中的 this 就指向了 window?
希望能大家的得到解惑。

<html>
<head>
<meta charset="utf8">
</head>

<body>
<button id="btn1">hello world</button>
<script type="text/javascript">
    document.querySelector("#btn1").addEventListener("click",hello);
    function hello()
    {
        console.log("hello 的 this")
        console.log(this)
        world();
    }
    
    function world()
    {
        console.log("world 的 this")
        console.log(this) // window
    }
</script>
</body>
</html>
回答
編輯回答
陌顏
2018年4月21日 00:37
編輯回答
尤禮

可以像你說的這么理解,函數(shù)默認(rèn)的this指向就是window,嚴(yán)格模式是null

2017年9月27日 18:52
編輯回答
好難瘦

當(dāng)函數(shù)做為對象的方法調(diào)用時(shí),this指向這個(gè)對象,當(dāng)函數(shù)直接被調(diào)用時(shí),this指向window,嚴(yán)格模式指向null。你的world方法是直接調(diào)用,所以指向window

2017年2月5日 13:18