admin管理员组文章数量:1389825
If I put a function into a string like this:
var functionString = function (message) {
console.log(message);
}.toString();
Is there any way to convert the string back to a function and call it? I tried
eval(functionString)
which returns "Uncaught SyntaxError: Unexpected token", and
functionString.call(this, "HI!");
which returns 'undefined is not a function'.
Is that even possible in javascript?
Thanks in advance for any reply!
EDIT: The point of this question is that the function has been converted into a string using toString(). So
console.log(functionString);
returns this string: "function (message) {console.log(message);}"
Can I transform the string back into a function and call it? That's the problem I am trying to solve. Thanks!
If I put a function into a string like this:
var functionString = function (message) {
console.log(message);
}.toString();
Is there any way to convert the string back to a function and call it? I tried
eval(functionString)
which returns "Uncaught SyntaxError: Unexpected token", and
functionString.call(this, "HI!");
which returns 'undefined is not a function'.
Is that even possible in javascript?
Thanks in advance for any reply!
EDIT: The point of this question is that the function has been converted into a string using toString(). So
console.log(functionString);
returns this string: "function (message) {console.log(message);}"
Can I transform the string back into a function and call it? That's the problem I am trying to solve. Thanks!
Share Improve this question edited Dec 5, 2014 at 21:19 user1840267 asked Dec 5, 2014 at 21:09 user1840267user1840267 4281 gold badge6 silver badges20 bronze badges 7- It would help if you'd post exactly what's in the string. – Pointy Commented Dec 5, 2014 at 21:09
- You could just do "return message.toString()" inside the function – ryanc1256 Commented Dec 5, 2014 at 21:12
- Is it possible, yes it can be, but what are you trying to do exactly? – epascarello Commented Dec 5, 2014 at 21:14
- stackoverflow./questions/912596/… – Brian Shamblen Commented Dec 5, 2014 at 21:19
- epascarello, I would like to convert the string back to a function. Brian, I don't want to call an existing function. I would like to convert the entire string into a function. – user1840267 Commented Dec 5, 2014 at 21:20
4 Answers
Reset to default 3You're nearly there, but you're missing something.
When we call toString() on your function, we get
"function (message) {
console.log(message);
}"
which we can then eval. However, we're just creating an anonymous function object here; we won't be able to call it!
If we instead to something like:
var functionString = "var restoredFunc = " + function (message) {
console.log(message);
}.toString();
We can then do the following
eval(functionString);
// prints "hello!" to console
restoredFunc("hello!");
Your functionString
contains exactly the string
"function (message) { console.log(message); }"
Evaluating it as-is does present JavaScript engine with incorrect syntax (there is no name for this function). JavaScript expects construct like function <name>(<params>) { }
. Alternatively, you can use anonymous function (i.e. no name present), but only as a parameter or in a context of evaluating expression. The minimal typical evaluating expression would be (function() {})()
If you want to get fancy, !function() {}
is also ok - the exclamation mark in front turns it into boolean expression that requires function evaluation before negating the output.
So, in your example this will work:
eval("("+functionString+")('abc')");
because then you do anonymous function call - something JavaScript can live with.
Alternatively, you can also use just brackets, then you need to assign the result to something you can use later:
var foo = eval("("+functionString+")");
foo('ddd');
Here is a little proof / playground to learn about it: http://jsfiddle/Exceeder/ydann6b3/
yes its possible in JavaScript but you can't eval
anonymous function without assignment
So you go through it like so
var functionString = function (message) {
console.log(message);
}.toString();
eval("myfunction =" + functionString)
myfunction("Hello World!")
Your functionString
is a string that looks like
"function (message) {
console.log(message);
}"
You could covert that string to an Immediately-Invoked Function Expression (IIFE) using string concatenation, similar to below.
(function (message) {
console.log(message);
})("HI!");
and then eval that. Here is the result from Chrome's JavaScript console:
eval('(' + functionString + ')("HI!")')
HI!
本文标签: Call javascript function encoded in a stringStack Overflow
版权声明:本文标题:Call javascript function encoded in a string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744671509a2618852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论