我正在定義一個Parent對象,並且我希望它具有一個具有自己的函數和私有變量的子成員對象。爲了封裝函數和變量,我給Parent原型添加了一個自動執行的匿名函數。從自我執行的匿名成員函數內部訪問父級原型範圍
這裏的代碼演示問題:
var Parent = function() {
this.memberVariable = 'hello world';
}
Parent.prototype.doSomething = function() {
return this.childObject.doSomething();
};
Parent.prototype.childObject = function() {
// instead of being Parent, `this` is the Window object. What is the best way to fix this?
var that = this;
return {
doSomething: function() {
// undefined, but should be 'hello world'
return that.memberVariable;
}
}
}();
var parent = new Parent();
console.log(parent.doSomething());
一個解決方法我是通過在父範圍內給孩子的功能,但是這似乎不可思議,而且好像有一定是一個更好的解決方案:
var Parent = function() {
this.memberVariable = 'hello world';
}
Parent.prototype.doSomething = function() {
// we pass in `this`
return this.childObject.doSomething(this);
};
Parent.prototype.childObject = function() {
return {
doSomething: function(that) {
return that.memberVariable;
}
}
}();
var parent = new Parent();
console.log(parent.doSomething());
有沒有更好的方法來實現這個目標?
你應該看看模塊/顯露的模塊模式... http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript – AdamJeffers
你不能有一個IEFE創建原型方法,並期望得到裏面有一個動態的'this'。 – Bergi
[組織原型javascript,同時保持對象引用和繼承](http://stackoverflow.com/q/15884096/1048572) – Bergi