[英]Access to instance public properties from prototype function (IIFE)
我正在尝试开发一个由更多部分组成的库,以使其正常运行。
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;
}());
您的上下文不是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();
您应该在public.of
内部创建self
变量:
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
因为self
是在匿名函数中而不是在任何对象的上下文中初始化的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.