javascript - console.log() returns undefined when I specify a function as its argument -



javascript - console.log() returns undefined when I specify a function as its argument -

i'm trying create site students allow them input javascript, run code, , test code against expected outputs. i'm having little bit of problem using function() constructor run code text input space.

a button pressed run submitcode()

function submitcode(){ $(document).find('textarea,:text').each( function(){ var codewritten = $(this).val(); console.log(codewritten); test(codewritten); }); }

the test method here:

function test(inputcode){ var passed = inputcode; //code wrote var args = 'pogchamp'; //this input want test code var f = new function('a',passed); console.log(passed); console.log('test'); console.log(f(args)); }

when check chrome output, value of passed shows properly, 'test', while console.log(f(args)); returns undefined.

here sample value of passed variable, reference:

function foo(str){ homecoming false; //auto-generated }

thanks, , sorry (i'm relatively new javascript)

new function() creates new function given contents. you're passing function declaration resulting function looks this:

function( ) { function foo(str){ homecoming false; //auto-generated } }

...which, when called, returns nothing. (you can confirm console.log(f.tostring())).

to prepare can utilize eval() instead:

eval( "var f = " + passed );

to inevitable "eval evil!!" comments: 1 of rare instances eval appropriate choice. new function() eval name. and, of course, need at least try..catch around grab syntax errors in input.

javascript jquery function return-value

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -