동적으로 결정된 이름의 개체에서 함수를 호출하는 데 문제가 있습니다. 아래 코드는 내 코드가 현재 어떻게 설정되어 있고 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