一、在浏览器里,this指向window
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<script>
var num = 100;
function test(){
console.log(this)
console.log(this.num)
}
test()
</script>
</body>
</html>
测试结果如下:
二、Node.js环境下this指向globel
var num = 100;
function test(){
console.log(this)
console.log(this.num)
}
test()
测试结果如下:
笔记素材来源:拉勾大前端训练营。