JavaScript - pass string variable to eval -


part of code generates string, suppose used function generated chuck in voxeljs.

an example string "(y == 1)? 1 : 0"

which need added function this, eg. "function(x, y, z){return (y == 1)? 1 : 0}"

i thought using eval change string needed code, this:

gtest = function(x, y, z){return eval(generationstring) }; 

but misunderstood how eval used, , realized needed try else.

if @ gtest in javascript console says it's structure function (x, y, z){return eval(generationstring) } when want function (x, y, z){return return (y == 1)? 1 : 0}}. attempting pass code containting eval causes voxeljs crash/freeze when tries generate new chunks.

how can convert string javascript code code in function in way want?

i realize might hard understand question, sorry that, i'm not sure how describe other way.

i found answer literally 30 seconds after posting this, use function() constructor.

given string describing javascript function, convert javascript function

gtest = new function('x', 'y', 'z', 'return ' + generationstring); 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -