zoukankan      html  css  js  c++  java
  • this指向

    1.全局环境下 的this 指向 window
    
    
    
    2. 函数的独立调用,函数的内部的this也指向了window
    
    function fn(){
    
      console.log(this);
    
    }
    
    fn();
    
    
    3. 当被嵌套的函数独立调用时候,this 默认指向了window
    var obj = {
    
      a:2,
    
      foo :function (){
        //函数当做对象的方法来调用 this指向了obj
        var that = this;     
    function test(){         console.log(this);         
             console.log(that.a)


        }   
      test();
      } }
    obj.foo();



    4. IIFE 自执行函数
    内部this指向window
    var a = 10;

    function foo(){
      console.log(this);
      (function test(that){
        console.log(that.a);
        console.log(this);

      })(this);
    }
    var obj = {
      a:2,
      foo:foo
    }
    obj.foo();



    5. 闭包 this 指向 window

    var a = 0;

    var obj = {
      a:2,

      foo:function(){

        var c = this.a;

        return function test(){

          console.log(this);
          return c;
        }
      }
    }
    var fn = obj.foo();
    console.log(fn());


  • 相关阅读:
    maven 手工装入本地包
    一个简单的算法--找出样本中出现次数最多的数字
    tortoise Git 访问题
    python 的数值
    python 的运算符
    python3代码运行器
    python 3.X基础
    Python 3.X和Python 2.X的区别
    文件操作
    函数讲解
  • 原文地址:https://www.cnblogs.com/angdh/p/13974428.html
Copyright © 2011-2022 走看看