this指向

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
            console.log(this) //window
            
            function cba(){
                console.log(this)
            }
            cba() //window
            window.cba()
            
//            function cc(){
//                console.log(this)
//            }
//            cc()
//            window.cc()
            
//            
            var a = {
                name:'a',
                abc:function(){
                    console.log(this)
                }
            }
            
            a.abc() //a
//            
//            
            var obj = {name:'obj'}
            obj.cc = cba
            obj.cc() //obj
//            
//            function add (){
//                this.name = 'xx'
//            }
//            
//            var tt = new add() //this-->tt
            
            //如何查看this的指向,看是哪一个对象调用了这个函数,这个this就只向谁,全局里面看window
        </script>
    </body>
</html>

 

posted @ 2019-03-18 21:34  就这样子吧  阅读(100)  评论(0编辑  收藏  举报