javascript - How to set a localStorage item as function? -
i set localstorage item function, when gets saved string.
localstorage.getdata = function (key="def") { let data = null; if (this[key] && !_this.isdataavailableinlist(this[key])) { data = this[key].data; } return data; };
the above function stored string , not function.
note: kindly not provide alternatives or other suggestions, aware of alternatives , curious know how works. have not provided entire context lets assume in need of setting localstorage item function
unfortunately local storage strings. arrays or other data converted strings.
for dead data without cycles can use of course json.stringify
, json.parse
, in other cases or code or other javascript entities unsupported json (like infinity
or undefined
) need serialize , deserialize yourself.
javascript provides eval
, can store function f
converting first string f+""
, function eval(s)
. won't work closures , cannot know if function indeed closure or not in javascript (so cannot raise error if impossible operation of serializing closure attempted).
Comments
Post a Comment