javascript - How can one determinate if a function will return sub-functions without execution -
it context of one-page application development, need transform string executable function.
var repo = {};
i used strings in 2 formats :
repo.toto = "function toto(){\ alert('hello, toto');\ }"; repo.titi = "function titi(){\ this.sub1 = function() {};\ this.sub2 = function() {};\ this.sub3 = function() {};\ }";
i want know if function once executed give, or not, object of sub-functions.
below full case illustrate problem. eval
not solution.
var createfunc = function(name) { var string = repo[name]; var func = function("return new "+string+""); repo[name] = func; }; typeof repo.toto; //string typeof repo.titi; //string createfunc('toto'); createfunc('titi'); typeof repo.toto; //function typeof repo.titi; //function repo['toto'](); //alert('hello, toto'); //object {} repo['titi'](); //object { sub1: anonymous/titi/this.sub1(), sub2: anonymous/titi/this.sub2(), sub3: anonymous/titi/this.sub3() }
do know way determinate if function give empty object (like in "toto" example) or not (like "titi" example), without having execute function?
i agree it's not possible every scenario.
but 2 sample inputs, main difference 1 has properties , other not.
note return new
you're instantiating functions strings , returning new instance. code directly inside function executed (like constructor) , result object of properties , methods. so... that's why first 1 returns empty object.
you tell if return empty object or not if there isn't this.(something)=
. might not best solution, don't see use of this.
Comments
Post a Comment