Is it possible to add a function in a json?

3

Let's get into the problem. In datatable.js, to display the columns I define as follows:

    "aoColumns": [
                { "mDataProp": "IdProcesso", "sTitle": "Numero" }
                 ]

If you need to perform some function:

                 {
                    "mDataProp": "DataAbertura", "sTitle": "Data Abertura",
                    "fnRender": function (oObj) {
                        return //minha function vem aqui!
                    }
                },

Now comes the question, is it possible for me to add this function to the Json object dynamically? When I say dynamic I mean the fact that this object will be created by another function and at the end it returns me a string generated by JSON.stringify and the function gets unusable. I hope I have been able to explain my doubt.

    
asked by anonymous 22.11.2016 / 18:59

1 answer

1

There is an alternative, using the function eval

The function eval executes the string passed to it, as you can see in the example below:

link

eval('alert("teste")');

This will cause the alert to appear on the screen. So my suggestion is that you save your function in string format, and execute it accordingly.

    
08.03.2017 / 17:18