jiangxujing11 发表于 2015-12-15 14:03:57

关于this和that的模糊概念

app.config(function(providerServices01Provider){
      providerServices01Provider.name = "zhangsan";
    })

    app.provider("providerServices01",function(){
      this.name = "";
      this.$get = function(){
      var that = this;
      var _name = "";
      var service = {};
      service.setName = function(name){
          _name = name;
      }

      service.getName = function(){
          return _name;
      }

         service.getConfigName = function(){
          return that.name+"123456";
      }

      return service;
      }

    })
这里为什么要用var that = this,不然返回的this.name就是未定义,必须用that.name?求指教

你懂的 发表于 2015-12-15 15:11:59

that 表示临时保存this指针,不然方法里面套方法,就不知道 this是谁了

jiangxujing11 发表于 2015-12-16 10:55:05

有用,谢啦
页: [1]
查看完整版本: 关于this和that的模糊概念