从原型函数(IIFE)访问实例公共属性

时间:2017-01-31 11:21:13

标签: javascript

我正在尝试开发一个由更多部件组成的库,以便正常运行。

var Brain = (function () {
    var publics = {
        studies: {
            "ManagedVehicles": "\\Brain\\Studies\\ManagedVehicles"
        }
    };

    publics.study = function (study) {
        if (! publics.studies.hasOwnProperty(study)) {
            throw new BrainInvalidStudy(study + " study does not exist.");
        }

        return new BrainStudy(study);
    }

    return publics;
}());

function BrainInvalidStudy(message) {
    this.message = message;
    // Use V8's native method if available, otherwise fallback
    if ("captureStackTrace" in Error)
        Error.captureStackTrace(this, BrainInvalidStudy);
    else
        this.stack = (new Error()).stack;
}

var BrainStudy = (function () {

    function BrainStudy (study) {
        this.study = study;
    }

    BrainStudy.prototype = (function (userId) {
        var publics = {};
        var self    = this;

        console.log(this);

        function query (data) {
            return $.ajax('/api/brain/study', {
                data: data
            });
        }

        publics.of = function (userId) {
            return new Promise(function (resolve, reject) {
                console.log(this); // window
                console.log(self); // window
                console.log(self.study); // undefined

                query({
                    what: self.study,
                    who:  userId
                }).done(function (response, textStatus, jqXHR) {
                    resolve(response);
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    reject();
                })
            });
        }

        return publics;
    }());

    return BrainStudy;
}());

BrainInvalidStudy.prototype = Object.create(Error.prototype);
BrainInvalidStudy.prototype.name = "BrainInvalidStudy";
BrainInvalidStudy.prototype.constructor = BrainInvalidStudy;  

问题是console.log(self.study)总是undefined,这阻止我使用基础Brain类的构造函数中定义的属性。

我已经阅读了其他类似的问题,但没有一个像我一样使用IIFE。

编辑:谢谢大家,我已经结合你的答案,这是最终的结果:

var BrainStudy = (function () {

    function BrainStudy (study) {
        this.study = study;
    }

    (function (prototype) {
        function query (data) {
            return $.ajax('/api/brain/study', {
                data: data
            });
        }

        prototype.of = function (userId) {
            var self = this;

            return new Promise(function (resolve, reject) {
                query({
                    what: self.study,
                    who:  userId
                }).done(function (response, textStatus, jqXHR) {
                    resolve(response);
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    reject();
                })
            });
        }
    }(BrainStudy.prototype));

    return BrainStudy;
}()); 

2 个答案:

答案 0 :(得分:1)

您的上下文不是BrainStudy,请尝试此

function BrainStudy (study) {
  this.study = study;
}

(function(proto) {
  function log(args) {
    console.log(args);
  }

  proto.of = function() {
    log(this.study);
  };

})(BrainStudy.prototype);

var b = new BrainStudy(12);
b.of();

function BrainStudy (study) {
  this.study = study;
}

BrainStudy.prototype = (function() {
  var proto = {};
  function log(args) {
    console.log(args);
  }

  proto.of = function() {
    var self = this;
    log(this.study);
    log(self.study);
  };
  return proto;
})();

var b = new BrainStudy(12);
b.of();

答案 1 :(得分:1)

您应该在self

中创建public.of变量
publics.of = function (userId) {
    var self = this;
    return new Promise(function (resolve, reject) {

        query({
            what: self.study,
            who:  userId
        }).done(function (response, textStatus, jqXHR) {
            resolve(response);
        }).fail(function (jqXHR, textStatus, errorThrown) {
            reject();
        })
    });
}

目前self等于window,因为它是在匿名函数中初始化的,而不是在任何对象的上下文中。