我使用了一个全局对象。我知道使用全局对象的缺点,但在这种情况下我想使用它。
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/
更多推荐
如何从本地范围创建基于实例的全局对象?
发布评论