12-6-上下文this

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    let a = function () {
        console.log(this);
    };
    console.log(a === window);


    // 无论是let还是var都是指向window的
    // let下的this并不是window的属性
    // 用let时this不等于window
    
    let zhangyu ={
        name:'张宇',
        x:function () {
            console.log(this);
        }
    };
    zhangyu.x(); // 对象方法自执行,this指向对象

    // 2层的时候  只会找父级
</script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/zhangyu666/p/11479679.html
今日推荐