如何从本地范围创建基于实例的全局对象?

编程入门 行业动态 更新时间:2024-10-28 04:30:50
本文介绍了如何从本地范围创建基于实例的全局对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用了一个全局对象。我知道使用全局对象的缺点,但在这种情况下我想使用它。

I have a single global object I use. I know the cons of using global objects but in this case I want to use it.

我将此全局对象称为对象管道b.c.它将我的模型分支到我的控制器,反之亦然...也许应该把它称为对象分支...反正......

I call this global object the object pipe b.c. it branches my models to my controller and vice versa...maybe should have called it object branch...anyways...

我犯的错误是我想的我只有一个模型在任何给定时间运行...但我没有,有多个。

The mistake I made is that I thought I only had one model running at any given time...but I don't, there are multiple.

因此我不能使用单个静态实现,我每个模型运行时需要和基于实例的一个,一个全局对象管道。

Hence I can't use a single static implementation, I need and instance based one, one global object pipe per each model running.

这是静态版本。 MC代表型号/控制器。

Here is the static versions. MC stands for Model/Controller.

/******************************************************************************************** * * MC - Model/Controller Types * *******************************************************************************************/ var MC = {}; /** ** Object Pipe */ MC.o_p = { model : 'default', result : 'continue', page : {}, args : {}, server : {}, hash : localStorage.hash };

我想做这样的事情:

MC.o_p1 = function() { return { model : 'default', result : 'continue', page : {}, args : {}, server : {}, hash : localStorage.hash } }

但现在返回对象在本地范围内与任何调用它的对象。

but now the return object is in local scope to whatever called it.

I需要基于全局实例的对象。

I need global instance based objects.

我不确定我是在考虑这个问题还是我在问什么可能?

I'm not sure if I'm over thinking this or what I'm asking is possible?

推荐答案

保管您的包裹私下,并且只有一些访问功能:

Hold your package privately and just have some access functions:

var myModel = (function() { var model_vars = { model: 'default', result: 'continue', page: {}, args: {}, server: {}, hash: localStorage.hash }; return function() { this.get = function(ele) { if (model_vars.hasOwnProperty(ele)) { return model_vars[ele]; } }; this.set = function(ele, value) { if (model_vars.hasOwnProperty(ele)) { return model_vars[ele] = value; } }; }; })();

然后你可以这样做:

Model = new myModel();

DEMO: jsfiddle/maniator/PSsQ3/

更多推荐

如何从本地范围创建基于实例的全局对象?

本文发布于:2023-11-14 21:41:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全局   实例   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!