Js查漏补缺09-this对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>this对象</title>
</head>
<body>
<!--1、为什么在函数里面可以直接打印this-->
<!--因为浏览器(解析器)在调用函数的时候-->
<!--会向函数的内部传递一个隐含的参数,这个参数就是this-->
<!--2、this是什么-->
<!--this:这个,这:指示代词-->
<!--this是代表函数(方法)所在的那个对象-->
<!--3、this的指向问题-->
<!--哪个对象调用方法,方法中的this就指向那个对象-->

<script>
    function f(){
        document.write(this);
        console.log(this)
    }

    f();

    var method={
        name:"Everything",
        fun:f,     //内部函数调用外部方法时只要写函数名即可,不用写()
        showName:function(){
            document.write("姓名:"+this.name)
        }
    };

    method.showName();

</script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/cuijunfeng/p/13167115.html
今日推荐