zoukankan      html  css  js  c++  java
  • var that = this 小坑记

    在js编码过程中,经常会使用如上的语句来规避拿不到变量的问题。

    比如:

    queryData:function () {
                var that=this;
                var param={};
                for(var key in this.condition){
                    if(this.condition[key]){
                        param[key]=this.condition[key];
                    }
                }
                AJAX.GET("/api/adream/flow/queryRoomsBySystem",param,function (data){
                    if(data.success && data.systemList != null){
                        var queryLen = data.systemList.length;
                        if(queryLen > 0){
                            for (var queryIndex=0; queryIndex<queryLen; queryIndex++){
                                debugger;
                                that.roomList.push(data.systemList[queryIndex]);
                            }
                        }
                    }
                });

    注意标黄的部分,如果用this.roomList,将会发现roomList为空对象,是因为this指向的是AJAX内部的对象,this会随着代码进入的层深来自动改变指向的对象,所以这里在用this.roomList,那确实拿不到外层的对象。

    而使用var that = this之后,that中的对象将是this刚进入queryData方法时候的副本,所以会拿到这个对象。

  • 相关阅读:
    一用就会的数据库
    MQ介绍
    SpringBoot之HandlerInterceptorAdapter
    Swagger2异常 java.lang.NumberFormatException: For input string: ""
    Could not get a resource from the pool
    spring boot 之监听器ApplicationListener
    Nexus的使用
    CentOS7 搭建maven私服Nexus
    centos7安装部署gitlab服务器
    centos7安装nginx
  • 原文地址:https://www.cnblogs.com/scy251147/p/10289132.html
Copyright © 2011-2022 走看看