如何在JSON中存储javascript函数 [英] How to store a javascript function in JSON

查看:82
本文介绍了如何在JSON中存储javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JS对象我想保存在Local Storage中以备将来使用,我无法将其解析为字符串。

I have a JS object I would like to save in Local Storage for future use, and I cannot parse it to a string.

代码:

JSON.stringify({
    a: 5,
    b: function (param) {
        return param;
    }
})

结果:

"{"a":5}"

如果没有使用JSON,如何保存以供将来使用?

How do I save it for future use, if not with JSON?

(创建我自己的Lexer-Parser以中断字符串函数我不认为是一种选择)

(And creating my own Lexer-Parser to interupt string function I dont think is an option)

推荐答案

通常这样的问题表明X / Y问题:你需要做X,你认为Y会帮助你这样做,所以你试着做Y,不能,并且问怎么做Y.询问如何做X更常有用。

Usually a question like this indicates an X/Y problem: You need to do X, you think Y will help you do that, so you try to do Y, can't, and ask how to do Y. It would frequently be more useful to ask how to do X instead.

但回答问题:你可以使用replacer和reviver函数将函数转换为字符串(在 stringify 期间)和回到一个函数(在 parse 期间)来存储函数的字符串版本,但是这样做有各种各样的问题,尤其是函数的作用域定义可能对函数很重要。 (这与您在问题中显示的功能无关,但我认为这并不具有代表性。)将字符串从本地存储转换为可能运行的代码意味着您相信本地存储内容没有'被恶意破坏了。除非该页面已经容易受到XSS攻击,否则不太可能,但这是一个值得注意的问题。

But answering the question asked: You could use replacer and reviver functions to convert the function to a string (during stringify) and back into a function (during parse) to store a string version of the function, but there are all sorts of issues with doing that, not least that the scope in which the function is defined may well matter to the function. (It doesn't matter to the function you've shown in the question, but I assume that's not really representative.) And converting a string from local storage into code you may run means that you are trusting that the local storage content hasn't been corrupted in a malicious way. Granted it's not likely unless the page is already vulnerable to XSS attacks, but it's an issue to keep in mind.

这是一个例子,但我不推荐它,除非其他选项已经用尽,尤其是因为它使用 eval ,它(就像它的近亲新函数))可以成为恶意代码的载体:

Here's an example, but I don't recommend it unless other options have been exhausted, not least because it uses eval, which (like its close cousin new Function)) can be a vector for malicious code:

// The object
var obj = {
    a: 5,
    b: function (param) {
        return param;
    }
};

// Convert to JSON using a replacer function to output
// the string version of a function with /Function(
// in front and )/ at the end.
var json = JSON.stringify(obj, function(key, value) {
  if (typeof value === "function") {
    return "/Function(" + value.toString() + ")/";
  }
  return value;
});

// Convert to an object using a reviver function that
// recognizes the /Function(...)/ value and converts it
// into a function via -shudder- `eval`.
var obj2 = JSON.parse(json, function(key, value) {
  if (typeof value === "string" &&
      value.startsWith("/Function(") &&
      value.endsWith(")/")) {
    value = value.substring(10, value.length - 2);
    return eval("(" + value + ")");
  }
  return value;
});
document.body.innerHTML = obj2.b(42);

这篇关于如何在JSON中存储javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆