zoukankan      html  css  js  c++  java
  • this 的指向问题

    1.全局作用域或者普通函数中 this 指向全局对象 window ( 定时器里面的this 指向 window )

     1.1 console.log(this); // window

     1.2 function fn(){

       console.log( this );  // window

      }

     fn();   // 实际上是  window.fn(); 所以 this 也是 window

     1.3 window.setTimeout(function(){

        console.log(this); // window  

      },1000)

    2.方法调用中谁调用 this 指向谁

     2.1 var o = {

       sayHi:function(){

         console.log(this);  //this 指向 o

       }

      }

     o.sayHi( );  // 因为是 o 调用了 sayHi 方法 所以 this 指向 o

     btn.onclcik = function (){

      console.log(this);  // btn

     }

    3.构造函数中 this 指向构造函数的实例

     function Fun(){

      console.log(this);   // this 指向的是 fun 的实例对象

     }

     var fun = new Fun( ) ;   // new 创建了一个新的实例对象 赋值给 fun ,所以 this 指向 fun

  • 相关阅读:
    Oracle数据库基础
    2016-08-08二期模拟考试
    易买网-登入
    常量接口模式
    反射
    Hhibernate延迟加载
    URL和URI的区别和联系
    Socket编程
    ArrayList如何实现线程安全
    移位运算符
  • 原文地址:https://www.cnblogs.com/qtbb/p/11693119.html
Copyright © 2011-2022 走看看