zoukankan      html  css  js  c++  java
  • this指向问题,只提供案例,不做任何分析

    希望大家在测试的道路上找到答案,阔步前行

    <script type="text/javascript">
                /*this指向
                
                console.log(this);
                 
                function fn(){
                    console.log(this);
                }
                fn();
                
                function Foo(){
                    this.name="jhon";
                    this.age=20;
                    console.log(this);
                }
                var f1=new Foo();
                
                function Foo(){
                    this.name="jhon";
                    this.age=20;
                    console.log(this);
                }
                Foo();
                
                var obj={
                    x:10,
                    fn:function(){
                        console.log(this);
                        console.log(this.x)
                    }
                }
                obj.fn();
                
                var obj={
                    x:10,
                    fn:function(){
                        function f(){
                            console.log(this);
                            console.log(this.x)
                        }
                        f();
                    }
                }
                obj.fn()
                
                var obj={
                    x:10,
                    fn:function(){
                        console.log(this);
                        console.log(this.x)
                    }
                }
                var fn1=obj.fn;
                fn1();
                
                var obj={
                    x:10
                }
                var fn=function(){
                    console.log(this);
                    console.log(this.x);
                }
                fn.call(obj);
                
                function Foo(){
                    this.name="jhon";
                    this.age=20;
                }
                Foo.prototype.getName=function(){
                    console.log(this,this.name);
                }
                var foo=new Foo();
                foo.getName();*/
                
            </script>

      

    1.谁最终调用函数,this指向谁。


      ①this指向的永远只可能是对象!!!


      ②this指向谁永远不取决于this写在哪,而是取决于函数在哪调用


      ③this指向的对象,称之为函数的上下文context,也叫函数的调用者

    2.this指向的规律(与函数调用的方式息息相关):


      this指向的情况,取决于函数调用方式有哪些,


      ①通过函数名()直接调用:this指向window


      ②通过对象.函数名()调用的:this指向这个对象


      ③函数通过数组的一个元素,通过数组下标调用的,this指向这个数组


      ④函数作为window内置函数的回调函数调用时,this指向window如setTimeout setInterval 等


      ⑤函数作为构造函数用new关键字调用时,this指向新new出的对象。

    如果您实在不愿意自行分析理解,或者基础较为薄弱无力进行分析:请移步

      王福朋-博客园

    吴建锐-博客园

  • 相关阅读:
    Vue自定义过滤器格式化数字三位加一逗号
    js复制功能
    vue中数据接收成功,页面渲染失败
    css技巧
    vue+elementUI table篇
    图片预加载
    jquery实现文字自动向上滚动,鼠标放上去停止,移开继续滚动代码...
    【Flutter】广播机制
    【Flutter】IOS打包
    【Flutter】BottomNavigationBar切换页面被重置问题(保存状态)
  • 原文地址:https://www.cnblogs.com/gitByLegend/p/10462175.html
Copyright © 2011-2022 走看看