zoukankan      html  css  js  c++  java
  • 了解javascript中的this --实例篇

    对javascript this的赋值有了深一层的理解后,看一下比较复杂的情况,this的应用篇参考《对javascript this的理解》.

    #demo1

     1 var name="window";
     2 var object = {
     3     name:"me",
     4     getname:(function(){
     5        alert(this.name);//window,加载即执行
     6         return function(){
     7             return this.name;
     8         }
     9     })()
    10 }
    11 alert(object.getname());// window  me

    首先getname 属性指向的值是一个自执行方法,为了方便,我们称它为AutoExeFun,首先激活AutoExeFun,并且进入到AutoExeFun的执行上下文AutoExeFunContext,并把AutoExeFunContext压入执行堆栈,可以很清楚的看到,AutoExeFun既不是标识符,也不属于属性访问器,所以  line 5 的 this指针为全局的global, 执行完之后返回function(){return this.name},然后由object.getname()调用,属于属性访问器访问,line7的this则为object

    #demo2

     1 var name="window";
     2 var object = {
     3     name:"me",
     4     getname:(function(){
     5         var that=this;
     6         return function(){
     7             return that.name;
     8         }
     9     })()
    10 }
    11 alert(object.getname());//window

    #demo2和#demo1的区别在于line5 中 定义了that作为自执行function中this的引用,由demo1分析可见,line5中的this指向全局对象,在return 的function中,用到了that局部变量,由于外部可能会调用,所以即使退出自执行function的上下文,依然会保持对that的引用,具体可参考《关于javascript中的闭包》 ,所以执行结果为window

    #demo3

     1 var name="window";
     2 var object = {
     3     name:"me",
     4     getname:function(){
     5         var that=this;
     6         return function(){
     7             return that.name;
     8         }
     9     }
    10 }
    11 alert(object.getname()());//me

    #demo3的执行分为两部分,第一部分object.getname(),标记为Section A, 很明显A部分function的调用 属于属性访问器类型,所以解析成对象引用后,会把base->object,同时this->base,即line5的this和that都是指object,Section B 部分, 同时由于闭包保持了对that的引用,在SectionB的执行上下文中,that依然指向object

    以上是几个关于this指针的demo,除全局的this外,function中this指针的赋值操作发生在function被调用的时候,即进入function所在的执行上下文期间。

    贴上一段关于context的伪代码描述,参考文章为 what-is-the-execution-context-in-javascript

    executionContextObj = {
        scopeChain: { /* variableObject + all parent execution context's variableObject */ },
        variableObject: { /* function arguments / parameters, inner variable and function declarations */ },
        this: {}
    }

    具体关于执行上下文的理解可参考 《关于javascript中的 执行上下文》

  • 相关阅读:
    chmod: changing permissions of ‘/etc/fstab': Read-only file system
    让Mac终端保持(SSH)与远程的连接状态
    修改数据库密码
    SSH密钥登录让Linux VPS/服务器更安全
    redis 使用redis Desktop manger进行远程进行链接
    mysql 性能
    对阿里云服务器(数据盘已分区并格式化)的数据盘进行扩容
    备份数据库的shell
    window上可以执行的shell脚本,复制到linux上执行报错了
    自然语言处理中的N-Gram模型
  • 原文地址:https://www.cnblogs.com/teamobaby/p/3845426.html
Copyright © 2011-2022 走看看