javascript - Trying to call function on a dynamically generated object name -
i'm having issues calling function on object name determined dynamically. code below illustrates how code set up, , problem i'm having occurring in function called dosomethingelse().
var obj = function(){ this.test = this.objmgr(); }; obj.prototype.objmgr = function(){ var self = this; function dosomething(){ //do processing seems unimportant particular prob dosomethingelse(); } function dosomethingelse(){ //the object need determined @ runtime, , therefore dynamic var callfunconthis = 'subobj'; //how heck can call function on object referenced in callfunconthis this[callfunconthis].a(); //doesn't work, refers dom window self[callfunconthis].a(); //doesn't work, self refers obj eval(callfunconthis).a(); //works, there better way? } var subobj = { a:function(){ }, b:function(){ } }; var subobj2 = { a:function(){ }, b:function(){ } }; dosomething(); return{ subobj:subobj, subobj2:subobj2 } }; var test = new obj();
subobj never assigned scope can reference. why eval() best solution. having said this, i'm looking @ 1 weird bit of code. can elaborate on little? feel there architectural problem.
also, since you're returning object contains subobj , subobj2, why not use scope.
function dosomethingelse(){ // ... retobj[callfunconthis].a() } var retobj = {'subobj': subobj, 'subobj2': subobj2}; dosomething(); return retobj;
Comments
Post a Comment