Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters in eval in an object form?

I have this json, and when i get this json i need to run the function which comes at callback object.

{
  formId: 'snn_service_item_form',
  item_id: '1',
  item_title: 'some item',
  item_description: '',
  item_duration: '10',
  item_price: '120',
  item_level_1: 1,
  item_level_2: 0,
  item_level_3: 1,
  item_type: 'p',
  callback: {
    callbackName: 'getServices',
    callbackParams: {
      _param1: 1,
      _param2: 2
    }
  }
}

so according to this i need to run this:

getServices(1,2);

i can do that with eval function like:

eval(json.callback.callbackName+'(\''+ json.callback.callbackNParams._param1 +'\',\''+ json.callback.callbackNParams._param2 +'\')');

i can automate this by putting it into a for in and writing parameters to a string, but i dont think this is the best way to go.

is there a way to assign function name from a var and giving its parameters as an object, in my case like:

json.callback.callbackName(json.callback.callbackParams);

i know this is not the way to do it but it is what i want to learn.

Thanks, Sinan.

like image 266
Sinan Avatar asked Sep 30 '09 21:09

Sinan


People also ask

Can I pass object as parameter JavaScript?

In JavaScript, you can use functions as values, just like numbers, strings, and objects. That means you can pass them as arguments, return them from other functions, and set them as properties of objects.

What is $$ eval?

$$eval() method. This method runs Array. from(document. querySelectorAll(selector)) within the page and passes the result as the first argument to the pageFunction .

Why eval is not recommended?

Malicious code : invoking eval can crash a computer. For example: if you use eval server-side and a mischievous user decides to use an infinite loop as their username. Terribly slow : the JavaScript language is designed to use the full gamut of JavaScript types (numbers, functions, objects, etc)… Not just strings!

Does eval return a value?

eval() returns the completion value of statementsFor if , it would be the last expression or statement evaluated. The following example uses eval() to evaluate the string str .


2 Answers

Depends on where the function to call is defined (global scope or a local scope).

If global, you don't need eval (and it's safer to avoid it), you just reference the function through the global window object:

var args = [];
for(var p in json.callback.callbackParams) {
    args.push(json.callback.callbackParams[p]);
}
window[json.callback.callbackName].apply(null, args)

See the apply() function used above.

If it's in a local scope, then you need the eval (how you have it is fine).

like image 173
Crescent Fresh Avatar answered Oct 22 '22 12:10

Crescent Fresh


Don't use eval. You can get a reference to a named global variable or function from the window object:

var callbackfunction= window[json.callback.callbackName];

And trying to serialise your values to a string just to have them parsed back to JavaScript unreliably is silly. Call the function explicitly:

callbackfunction.call(window, json.callback.callbackParams.param1, json.callback.callbackParams.param2);

(window here is a dummy value for this for when you're not using object methods.)

Better for automating it to accept any number of parameters would be to turn callbackParams into a plain Array:

callbackParams: [1, 2]

and then use apply to call the function:

callbackfunction.apply(window, json.callback.callbackParams);
like image 21
bobince Avatar answered Oct 22 '22 12:10

bobince