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>

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

  王福朋-博客园

猜你喜欢

转载自www.cnblogs.com/gitByLegend/p/10462175.html