I work with some objects that contain several data that will be displayed and managed from a browser, and I want to save them in local storage. I used JSON.stringify () to save the objects, so everything becomes text, and it works well
{ "bindingRef": [], "primo": { "name": "primo", "modifiable": true, "binded": false, "isInteger": false, "label": "Numero di Primi" }, "secondo": { "name": "secondo", "modifiable": true, "binded": false, "isInteger": false, "label": "Numero di Secondi" } }
Now I am trying to save a function as well, converting it to a string and saving it
JSON.stringify(myFunction.toString());
but the way out is
"savedFunction": "function () {\n\t\t\t\tvar tot = menu.primo.get() * 6 + menu.secondo.get() * 8 + menu.dolce.get() * 4;\n\t\t\t\tif (menu.sconto.get()) {\n\t\t\t\t\treturn tot * 0.90;\n\t\t\t\t} else {\n\t\t\t\t\treturn tot;\n\t\t\t\t}\n\t\t\t}"
Is it correct to save the function in local storage or is there a better way to do this? If this is the correct way, is there a way to simply remove any tab / indent character, or should I manipulate the line, for example using some regular expression function?
source share