名前が動的に決定されるオブジェクトで関数を呼び出すときに問題が発生します。以下のコードは、私のコードが現在どのように設定されているかを示しています。問題は、doSomethingElse()という関数で発生しています。動的に生成されたオブジェクト名で関数を呼び出そうとしています
var Obj = function(){
this.test = this.objMgr();
};
Obj.prototype.objMgr = function(){
var self = this;
function doSomething(){
//do some processing that seems unimportant to this particular prob
doSomethingElse();
}
function doSomethingElse(){
//The object that I need is determined at runtime, and is therefore dynamic
var callFuncOnThis = 'subObj';
//How the heck can I call function a on the object referenced in callFuncOnThis
this[callFuncOnThis].a(); //Doesn't work, this refers to dom window
self[callFuncOnThis].a(); //Doesn't work, self refers to obj
eval(callFuncOnThis).a(); //Works, but is there a better way?
}
var subObj = {
a:function(){
},
b:function(){
}
};
var subObj2 = {
a:function(){
},
b:function(){
}
};
doSomething();
return{
subObj:subObj,
subObj2:subObj2
}
};
var test = new Obj();
タイトルに質問を簡単に記入してください。 *別のJavascript OOの質問*は実際にそれをしません。 – alex