如何分享两个页面之间的对象? [英] How to share an object between two pages?

查看:213
本文介绍了如何分享两个页面之间的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的JS脚本来创建一个对象,只允许一个实例的inizialization。如果该对象已经inizialized它返回时没有再重新创建它。

I wrote a simple JS script to create an object that permit the inizialization of only one instance. If the object is already inizialized it return is without recreate it again.

这是我的: myobj.js

var Singleton = {

  initialized : null,

  init: function(){

    console.log("new obj...");

    this.initialized = {
      'status' : 'initialized'
    }

  },

  getInstance : function(){

    if (!this.initialized) this.init();

    return this.initialized;

  }

}

然后,我有创建的test.html页面来测试这个脚本:

Then I have create a test.html page to test this script:

<script src="myobj.js"></script>

<script>
var uno = Singleton.getInstance();
var due = Singleton.getInstance();

(uno===due) ? console.log("equals") : console.log("not equals");
</script>

所有的作品好,只创建一个对象。

All works good, only one object is created.

我的问题是:我可以分享更多的HTML页面之间的对象单身

My question is: can I share this object "Singleton" between more HTML pages?

无需重新创建它在不同的页面?

without recreate it in different pages?

如果我有(例如)100上的标签浏览器中打开,我想用相同的对象,而不必同100个对象。

If I have (for example) 100 tabs opened on the browser, I would like to use the same object without having the same 100 objects.

感谢您!

推荐答案

有可能没有办法现在就这样做,但不要完全放弃希望;有可能是一个方式很快做到这一点。有几个很好的理由为希望分享页面之间的JS对象,但他们中的一个,我也想过是分享大的持久数据结构的多个WebWorkers之间的。

There may not be a way to do it now, but don't completely give up hope; there may be a way to do this soon. There are several good reasons for wanting to share JS objects between pages, but one of them that I've thought about is sharing large persistent data structures between many WebWorkers.

下面是从Berkeley两篇论文就这一话题,从2010年,所以你可以看到,你是不是谁想要它是唯一一个...

Here are two papers from Berkeley on this very topic, from 2010, so you can see that you're not the only one who wants it...

伯克利/谷歌:对象视图:细粒度共享的浏览器

伯克利/微软:安全的协作共享
JavaScript中,浏览器和物理资源

这篇关于如何分享两个页面之间的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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