I am currently working with objects that contain data to be displayed and manipulated in a browser. I want to store this data in local storage. To achieve this, I have been using JSON.stringify() to convert the objects into text, which has proven successful.
{
"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 attempting to save a function by converting it to a string before storing it:
JSON.stringify(myFunction.toString());
However, the output looks like this:
"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 this the correct method for saving a function in local storage? Are there alternative approaches that may be more effective? If this is indeed the right way, is there a simple way to remove any tabs or indentation from the string, perhaps through the use of a regular expression?