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.

Yamit
  • 465
  • 2
  • 6
  • 19

3 Answers3

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();
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 1
    Also bear in mind this is dangerous and can allow people to inject javascript into your page. – Liam Jan 22 '14 at 16:47
  • 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](http://stackoverflow.com/questions/2001449/is-it-valid-to-define-functions-in-json-results) – Augusto Destrero Jan 22 '14 at 16:53
  • +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) – Frogmouth Jan 22 '14 at 16:55
  • 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. – Yamit Jan 22 '14 at 16:57
  • 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. – Fenton Jan 22 '14 at 17:01
  • thanks for your comments, this is my solution http://jsfiddle.net/6WM8G/ using eval. – Yamit Jan 22 '14 at 19:05
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.

friedi
  • 4,350
  • 1
  • 13
  • 19
1

'eval' will do it:

var obj = { "action": "alert('test');" };
eval(obj.action);
XPX-Gloom
  • 601
  • 5
  • 11