2

我需要在 LocalStore 中存储一个函数的实例,例如:

function boo() {
   //blah blah
}
var instance = new boo(); //I need to store this variable

如何instance在 LocalStorage 中存储变量?

编辑:

假设我需要在所有浏览器的选项卡/窗口中共享同一个函数实例,就像所有浏览器选项卡的单例一样。

4

2 回答 2

-1

Attempting to store the function itself is unnecessary. The function is static code, not an instance of data and should be passed to the browser from your server.

Also, localStorage is separated by domain, so if your goal is to inject a function into a user's browser and then call it on other websites this is not possible. What you need for that is to create a browser plugin and submit it to the various stores.

The Javascript env per page is also not shared between tabs.

于 2013-04-13T11:37:52.470 回答
-4

你能做的是

var instance = this["boo"];

这将保存函数,但会忽略上下文。所以你必须将上下文保存在一些单独的变量中,或者使函数独立于上下文并将state一些保存在哪里

保存整个上下文

var context = JSON.stringify(this);
now when you retrive it then just use eval
var context = eval(context);

instance.cal(context);
于 2013-04-13T11:15:59.433 回答