After no help via Google and a post, this is my guess:
var MC = {};
MC.o_p = function ( type ) {
return {
model : type,
result : 'continue',
page : {},
args : {},
server : {},
hash : localStorage.hash
}
};
which I call like this
var obj1 = MC.o_p( 'MUserTry' );
Here the object o_p is is “scoped” to MC. You can create multiple instances via the call above. The string MUserTry is passed in as the variable type and set to the object property model.
Will this work? I’m taking a break from trial and error at the moment, but what I need is to be able to create object literals of a certain form with multiple instances.
Related
How to create global, instance based objects from local scope?
You can’t scope the resulting object. The
o_pproperty is a member of an object, but the object that is returned by the function is not.An object literal is just the
{ ... }syntax used to create objects, so what you want to do is to create an object, not an object literal. (Creating an object literal would be done by putting code for an object literal in a string, and execute the string.)You are misusing the constructor function, but it will work. An object will be created, and the function will be called to initialise it, then the object will be discarded and replaced by the object that the function returns.
Just remove the
newkeyword, so that you make a regular function call, and it will return the object.