0

Is it possible to make something like this without using eval?

foo1 = {"x": 4};
foo2 = {"x": "someFunc(foo1.x)"};

someFunc(var1)
{
    return (var1 + 1);
}

alert(foo1.x); // 4
alert(foo2.x); // 5 (hopefully)

Actually this is two problems. The first is to get a function to execute and the second is to do so without the parentheses, because in a loop I don't know if it's foo2.x or foo2.x().

The best thing I can think of is to search the JSON objects for keywords representing functions and use a switch list to execute them and replace the keywords with the value.

The disadvantage is that I need to update the JSON object every time something changes.

7
  • Why not wrap it inside another function like {"x": function () { someFunc(foo1.x); } } Commented Mar 6, 2013 at 15:40
  • 1
    @Салман — That is invalid JSON. Commented Mar 6, 2013 at 15:41
  • Does it need to be JSON, or just a JavaScript object? Commented Mar 6, 2013 at 15:41
  • @Quentin Oh, is it! But why is it allowed in browsers? Commented Mar 6, 2013 at 15:42
  • 1
    @Салман: Because it's not JSON. It's a JavaScript object. Commented Mar 6, 2013 at 15:43

3 Answers 3

1

You just want a normal javascript object:

var foo1 = {x: 4};
// we make foo2.x a function so it will dynamically update with foo1.x 's value
var foo2 = {x: function(){ 
    return someFunc(foo1.x);}
};

function someFunc(var1)
{
    return (var1 + 1);
}

alert(foo1.x); // 4
alert(foo2.x()); // 5

If you need to then pass those objects as JSON you can use JSON.stringify to pass the object to a server.

JSON isn't intended to be dynamic, its a language for passing data. You should represent your data as an object while you're manipulating it, and then can always convert it to JSON if you need to send it.

Sign up to request clarification or add additional context in comments.

3 Comments

You should understand you were never working with JSON to begin with.. you are using Javascript object initializer which looks like JSON but is not.
The original code was var foo1 = {x: 4}; var foo2 = {x: someFunc(foo1.x)}; function someFunc(var1) { return (var1 + 1); } alert(foo1.x); // 4 alert(foo2.x); // 5 (hopefully) But the edited code doesn't work for me, also it uses the parentheses again.
sorry, misplaced a semicolon. Fixed now. Feel free to use the original if its working for you too.
1

JSON has no built-in function type.

Your options:

  • Eval (which is evil)
  • Preload your functions onto your page (e.g. as methods on an object), then pass the name of the function in the JSON. (You can then call myObject[function_name_as_string_from_json](foo, bar)
  • Write a DSL and a JS parser for it (if your functions vary in simple ways)

3 Comments

The op is actually just talking about javascript and javascript object literals
@Esailija — I'm not so sure. All the talk of eval makes me think the code given is a simplified example that cuts out the process of fetching and parsing the JSON.
I dunno, "JSON Object" sets the alarm for me as always.
0

Not in any straightforward way. Imagine any algorithm that might do it. Then there would be no straighforward way for the algorithm distinguish this data:

foo1 = {"x": "someFunc(foo2.x)"};
foo2 = {"x": "someFunc(foo1.x)"};

function someFunc(var1) {
    return (var1 + 1);
}

It's difficult to come up with an algorithm that would stop this infinite loop, or a less direct one.

3 Comments

This is not an answer. It's actually a question.
@dragon112: The questions were of course rhetorical. Nonetheless, I edited to change the tone from questions to statements. But the content is still the same; my answer is that there is no reasonable way to do this. I can come up with hacks that might seem to work, but then I can easily come up with scenarios that would break them.
I agree to your argument, just not the way it was written down before.

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.