Js, Turn String To Function
I want to know how can i turn string into a wrap function. I have a string function data var a = new String('alert(turned)'); And I want to turn a into a wrapped function a = func
Solution 1:
You can also use the function constructor with the new
keyword like this.
var a = new String("alert('hello')");
a = new Function(a)
a()
Would result in an alert box with 'hello'
You can even make a new function using this method which accepts arguments. see here https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function
But I would be careful about what you turn into a function. You don't want users to be able to execute code.
Solution 2:
var a = eval (" return function() { alert('yo')}; ");
But don't
Post a Comment for "Js, Turn String To Function"