学习自原文  http://www.cnblogs.com/pssp/p/5216085.html后的一点小结(原文作者总结的很棒^_^)!
  关于js中this的指向,在函数定义的时候还无法确定,只有在函数执行的时候才能确定this到底指向谁,实际上this最终指向的是调用它的对象。
eg1:

function a(){
    var user = "xxxx";
    console.log(this.user); //undefined
    console.log(this); //Window  }

a();//此处和执行window.a()是一样的效果
··········································································································································································································································································
eg2:
var o = {
    user:"xxxx",
    fn:function(){
        console.log(this.user);  //xxxx   
    }
}
o.fn(); //此处和执行window.o.fn();是一样的效果。 这里的this指向的是对象o,因为你调用这个fn是通过o.fn()执行的,那this自然指向的就是对象o
···········································································································································································································································································
eg3:
var o = {
    a:10,

    b:{
        fn:function(){
            console.log(this.a); //undefined   }

}
}

o.b.fn();//尽管对象b中没有属性a,这个this指向的也是对象b,因为this只会指向它的上一级对象,不管这个对象中有没有this要的东西。
··········································································································································································································································································
eg4:
var o = {
    a:10,
    b:{
        a:12,
        fn:function(){
            console.log(this.a); //undefined
            console.log(this); //window        }
    }

}
var j = o.b.fn;//将对象o的属性b的方法即函数fn赋给变量j,此时并没有执行;
j(); //加了一个()立即执行函数fn,此处和执行window.fn();是一样的效果,此时this指向的是window。this永远指向的是最后调用它的对象,也就是看它执行的时候是谁调用的, 这句话至关重要。
     例子4中虽然函数fn是被对象b所引用,但是在将fn赋值给变量j的时候并没有执行所以最终指向的是window,这和例子3是不一样的,例子3是直接执行了fn。
··········································································································································································································································································
eg5:
var name = "The Window";
  var object = {
    name : "My Object",
    getNameFunc : function(){
      return function(){
        return this.name;
      };
    }
  };
 console.log(object.getNameFunc( )( )); // The Window
     这里object.getNameFunc( )( )会先执行object.getNameFunc( ),返回function( ){ return this.name; }这个函数,函数function(){ return this.name;}后面跟了一个( )会执行这个函数,此时这里的this指向的是window对象,所以this.name的值就是The Window。
··········································································································································································································································································
eg6:
 var name = "The Window";
  var object = {
    name : "My Object",
    getNameFunc : function(){
      var that = this;
      return function(){
        return that.name;
      };
    }
  };
console.log(object.getNameFunc()()); // My Object
     这里object.getNameFunc( )( )会先执行object.getNameFunc( ),返回function( ){ return this.name; }这个函数,函数function(){ return this.name; }后面跟了一个( )会执行这个函数,此时这里的this被赋值给了that,改变了this的指向,此时的this指向的是object对象,而不是window对象,所以this.name的值就是My Object 。
 
备注:

(function(){
....
})()

第一个括号是个运算符,它会返回这个匿名函数,然后最后一个小括号会执行这个函数。

······································································································································································································································································
结论:
  情况1:如果一个函数中有this,但是它没有被上一级的对象所调用,那么this指向的就是window。这里需要说明的是在js的严格版中this指向的不是window,严格模式下禁止this关键字指向全局对象,严格模式下,this的值为undefined。

  情况2:如果一个函数中有this,这个函数有被上一级的对象所调用,那么this指向的就是上一级的对象。

  情况3:如果一个函数中有this,这个函数中包含多个对象,尽管这个函数是被最外层的对象所调用,this指向的也只是它上一级的对象,

理解js中this的指向的更多相关文章

  1. 彻底理解js中this的指向,不必硬背。

    首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...

  2. 彻底理解js中this的指向

    首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...

  3. 【转发】彻底理解 JS 中 this 的指向

    为什么要学习this?如果你学过函数式编程,面向对象编程,那你肯定知道干什么用的,如果你没有学过,那么暂时可以不用看这篇文章,当然如果你有兴趣也可以看看,毕竟这是js中必须要掌握的东西. 例子1: f ...

  4. 【转】彻底理解js中this的指向,不必硬背。

    首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...

  5. 如何简单理解js中this的指向

    前序 每个人学js都会被this指向这个东西搞得很蒙,那就是this的指向问题.首先,我们要明白 this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上thi ...

  6. 彻底理解js中this的指向,不必硬背

    来自   https://blog.csdn.net/u011088260/article/details/79230661 首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行 ...

  7. 彻底理解js中this的指向,不必硬背(转)

    转自: http://www.h5cn.com/js/jishu/2016/0226/18248.html 首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定th ...

  8. 彻底理解 JS 中 this 的指向

    首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...

  9. 如何更好的理解js中的this,分享2段有意思的代码

    关于js中this的浅析,大家可以点击[彻底理解js中this的指向,不必硬背]这篇博客了解. 今天遇到2段比较有意思的代码. ----------------第一段----------------- ...

随机推荐

  1. BZOJ4004: [JLOI2015]装备购买

    总之就是线性基那一套贪心理论直接做就好了. 然而加强数据后很卡精度的样子. 于是重点在于这个特技:在整数模意义下搞. #include<cstdio> #include<algori ...

  2. ASP.NET程序单客户端(浏览器)登录的实现方案

    需求描述:当用户的账户在另一个浏览器中登录的时候,需要把当前浏览器的登录强制下线.这种需求在业务系统,或付费视频服务网站中比较常见. 这种需求我称之为"单客户端(浏览器)"登录,与 ...

  3. C#获取C++中修改过的float数组(指针),dll

    C++中 struct rankPoint{ float sim; }; ]){ ; i < ; i++) prank[i].sim = ; ; i < ; i++) prank[i].s ...

  4. Git创建ssh-key

    打开git bash界面,输入: ssh-keygen -t rsa -C "yourname@email.com" 一路回车,后续保持默认值即可. 把C:\users\yourn ...

  5. jquery控制滚动条改变上面固定(fixed)导航条或者搜索框的属性

    <script type="text/javascript"> $(document).ready(function(){ $(window).scroll( func ...

  6. scrapy2_初窥Scrapy

    递归知识:oop,xpath,jsp,items,pipline等专业网络知识,初级水平并不是很scrapy,可以从简单模块自己写. 初窥Scrapy Scrapy是一个为了爬取网站数据,提取结构性数 ...

  7. activity栈的关系

    android:intent flags 一.Activity和Task(栈)的关系 Task就像一个容器,而Activity就相当与填充这个容器的东西,第一个东西(Activity)则会处于最下面, ...

  8. Robot Framework--02 菜单栏&工具栏

    转自:http://blog.csdn.net/tulituqi/article/details/7584795 我把RIDE的界面大致分了四个区域:菜单栏.工具栏.案例及资源区.工作区,如下图 菜单 ...

  9. VM EXSI安装使用

    1.下载VM ESXI:http://lookdfw.blog.163.com/blog/static/5824974220139295524473/ 2.安装VM ESXI: 参考网址:http:/ ...

  10. mysql之用户授权

    授权命令: 1.全部权限:grant all on *.* to user@192.168.10.2 identified by "pass": 2.部分权限:grant sele ...