【问题标题】:Javascript Class and scopeJavascript 类和范围
【发布时间】:2013-10-03 14:47:51
【问题描述】:

我想在 javascript 中创建一个类,以便在与 indexeddb 对象的连接中重用我的主代码。我现在拥有的是:

function DATABASE() {
    this.DB_NAME = 'MYdatabase';
    this.DB_VERSION = 1;
    this.db = null;
    this.results = null;
}

DATABASE.prototype.open = function(callback) {
    var req = indexedDB.open(this.DB_NAME, this.DB_VERSION);

    req.onsuccess = function (evt) {
        this.db = this.result;
        callback();
    };

    req.onerror = function (evt) {
        console.error("openDb:", evt.target.errorCode);
    };

    req.onupgradeneeded = function (evt) {
        console.log("openDb.onupgradeneeded");        
    };
}

我的问题是,当 onsuccess 执行时,我失去了主类的范围,这不是我所期望的。我怎样才能做我正在寻找的东西? 我想同时与此建立一些联系,例如:

var DB = new DATABASE();
DB.open(function(res){});

var DB2 = new DATABASE();
DB2.open(function(res){});

var DB3 = new DATABASE();
DB3.open(function(res){});

非常感谢。

【问题讨论】:

    标签: javascript class scope indexeddb


    【解决方案1】:

    在var req 下添加var self = this; 并在范围更改时像这样使用:

    self.db = self.result;
    

    【讨论】:

      【解决方案2】:

      我的问题是,当 onsuccess 执行时,我失去了主类的范围,这不是我所期望的。

      不是作用域,而是函数调用期间this 的值取决于函数的调用方式。所以发生的情况是,您分配给 req 的函数被调用,而 this 的值与调用 open 时的值不同。

      我怎样才能做我正在寻找的东西?

      由于您的函数已经超出了对open 的调用范围,因此最简单的方法是执行what Andy suggested:

      DATABASE.prototype.open = function(callback) {
          var req = indexedDB.open(this.DB_NAME, this.DB_VERSION);
          var self = this;                                        // <=== New
      
          req.onsuccess = function (evt) {
              self.db = this.result;                              // <=== Changed
              callback();
          };
      
          // ...
      }
      

      注意:在更改的行中,我不知道this.result是什么,所以我也不知道是否将this更改为self。如果result 是this 在回调中指向的对象的属性,那么您完全有可能真正想要this.result。

      更多:

      【讨论】:

        【解决方案3】:

        这对你有用吗?将 open 函数放在 DATABASE 中,而不是放在原型中。

        function DATABASE() {
          var _this=this;
          _this.DB_NAME = 'MYdatabase';
          _this.DB_VERSION = 1;
          _this.db = null;
          _this.results = null;
        
          _this.open = unction(callback) {
            var req = indexedDB.open(_this.DB_NAME, _this.DB_VERSION);
        
            req.onsuccess = function (evt) {
              _this.db = _this.result;
              callback();
            };
        
            req.onerror = function (evt) {
              console.error("openDb:", evt.target.errorCode);
            };
        
            req.onupgradeneeded = function (evt) {
              console.log("openDb.onupgradeneeded");        
            };
          }
        }
        

        【讨论】:

        • 非常感谢大家,我想我会使用ChrisC的方法。再次感谢您提供的有用帮助。
        【解决方案4】:
        var that = this
        req.onsuccess = function (evt) {
            that.db = that.result;
            callback();
        };
        

        另外我推荐阅读这篇文章:Scope and this in JavaScript

        【讨论】:

          猜你喜欢
          • 2010-12-21
          • 2013-01-16
          • 2015-08-17
          • 2021-01-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多