2

in JAVA I generate a String like this:

{
 "name":"ali",
 "lastName":"cate",
 "action":"function(event) {alert(this.name +'clicked');}"
}

and I can use this String pretty easy in javaScript like a JSON object, however i can't execute the function ( in JS) that is described in the action property, I had this error:

 TypeError: Object function(event) {alert(this.name +'clicked');}
 has no method 'apply'

so... is there any way to make it work?

EDIT (more info)

the problem is that the Json String is generated in Java (with the quotes). and this is and example, actually the real problem is with a highchart pie graph, this recieves the function in an event property and is it who tries to run that.

7
  • 3
    I believe eval() will do this, but it's generally considered bad practice to use. Commented Jan 22, 2014 at 16:42
  • 1
    It's valid JSON, but wouldn't the function be parsed as a string, and not a function? Commented Jan 22, 2014 at 16:44
  • It should, that's why I'm puzzled by that Object function... message. Commented Jan 22, 2014 at 16:45
  • 3
    You really should refactor this so that the logic is delivered to the client up front as regular JavaScript and the JSON contains only data. Commented Jan 22, 2014 at 16:45
  • 2
    possible duplicate of Execute JavaScript code stored as a string Commented Jan 22, 2014 at 16:45

3 Answers 3

4

You can do it without eval by dropping the quotes around the function...

var obj = {
 "name":"ali",
 "lastName":"cate",
 "action": function(event) {alert(this.name +'clicked');}
};

obj.action('');

Bear in mind that this will not be the object if you do this from a real event - it will be the element clicked, for example.

From a design perspective, does the function need to be part of the object?

function objectClicked(obj) {
    alert(obj.name + ' clicked');
}

You could pull the function out and have that in your own JavaScript and then pass a simpler object:

{
 "name":"ali",
 "lastName":"cate"
};

If you are really detemined about using eval... Wrap the function in a self-executing anonymous function, then assign the eval result of this to the action, which will result in action just being a normal function from then on...

var obj = {
   "name":"ali",
   "lastName":"cate",
   "action":"(function() { return function(event) {alert(this.name + ' clicked');} }())"
};

obj.action = eval(obj.action);

obj.action();
Sign up to request clarification or add additional context in comments.

6 Comments

Also bear in mind this is dangerous and can allow people to inject javascript into your page.
Opener said that he produced the JSON string in Java and want to execute the javascript function on the client side. Dropping the quotes around the function makes the string an invalid JSON to transfer from server to client. See i.e. this question on SO
+1... but only if use second method. functionality in JSON is not a good idea, for me. Put the function on ajax or JSON parsing function callback. - i quote @Quentin (see his comment above)
well the problem is that the Json String is generated in Java (with the quotes). and this is and example, actually the real problem is with a highchart pie graph, this recieves the function in and event property and is it who tries to run that.
I have added an eval option. I recommend wrapping it so the result of the eval is the required function. Without wrapping it, the eval won't work as expected.
|
2

This error occurs, because action is a string and not a function.

You can get it to work with eval, but this is bad practise.

Comments

1

'eval' will do it:

var obj = { "action": "alert('test');" };
eval(obj.action);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.