Save function to localstorage

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?

+6
source share
2 answers

A function in JS, like in many functional languages, is a closure: they include the contents of the environment at the time of definition, including ephemeral data such as db or file descriptors.

This is not a good idea, because it can lead to problems due to the behavior of JSON deserialization, so you should check what is completed in this function and that it is self-determining.

See also this SO stream for more information.

+5
source

You can put a function in an object and use the storage.set and storage.get functions that I created instead of localStorage.set and localStorage.get (localStorage does not allow adding functions, unlike JSON).

storage.set will pull together an object, including functions, before using localStorage.setItem ().
storage.get will parse the object, including functions after using localStorage.getItem ().

I changed the JSON.stringify and JSON.parse functions to be able to handle functions, so you can use it in other parts of your code without changing the function names. I added 2 to the original functions, so I can use them in updated functions.

 JSON.stringify2 = JSON.stringify; JSON.parse2 = JSON.parse; JSON.stringify = function(value) { return JSON.stringify2(value, function(key, val) { return (typeof val === 'function') ? val.toString().replace(/\t|\n/g, '') : val; }); } JSON.parse = function(value) { return JSON.parse2(value, function(key, val) { if (typeof val === 'string') { var regex = /^function\s*\([^()]*\)\s*{.*}$/; if (regex.exec(val) !== null) return eval('key = ' + val); else return val; } else return val; }); } var storage = {}; storage.set = function(key, value) { if (typeof value === 'object') value = JSON.stringify(value); localStorage.setItem(key, value); } storage.get = function(key) { var value = localStorage.getItem(key); try { return JSON.parse(value); } catch (e) { return value; } } 
+1
source

Source: https://habr.com/ru/post/918239/


All Articles