1

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?

Community
  • 1
  • 1
  • I'm not quite sure want to do, but yes, that "works". You can also call the function without `new`, which might be more appropriate. Btw, you don't "create" object literals, you are *using* to create *objects*. You can of course also use a properly set up constructor function to create instances, that's what they are there for. Maybe this helps: https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects#Creating_new_objects – Felix Kling Jul 30 '12 at 22:56
  • O.K, I've never used something like this before...but I thought the syntax would have been simpler...hence a bit of paranoia about doing it this way. –  Jul 30 '12 at 22:57
  • Objects have no scope. An object is oblivious to its original variable scope, and can make no particular use of it. –  Jul 30 '12 at 22:58
  • well o_p needs 'MC.' in front of it to work...I call that scope...what do you call it (updated question)? –  Jul 30 '12 at 22:59
  • 1
    It's usually called namespace. Scope is for variables. Namespacing is collecting data on a single object as properties. The object is then referenced by a variable, but the object is completely unaware of any variable scope. –  Jul 30 '12 at 23:00
  • I just can't seem to grasp that there is not a construct for creating instance based object literals...they have to be wrapped in objects. –  Jul 30 '12 at 23:10
  • You cannot create "object literals". Literals are used to create one instance of something, whereas constructor functions are used to create multiple instances of something. If you want multiple objects, then use constructor functions properly. – Felix Kling Jul 30 '12 at 23:26

1 Answers1

1

You can't scope the resulting object. The o_p property 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 new keyword, so that you make a regular function call, and it will return the object.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • I guess what I'm confused about...is that there is no natural construct for creating instance based object literals {}, you have to wrap them in objects...is this correct? –  Jul 30 '12 at 23:07
  • @HiroProtagonist: You just broke the code, you can't use `function MC.o_p` to define a function in the `MC` object. What do you mean by "instance based object literals"? An object literal is just a syntax for creating object instances. – Guffa Jul 30 '12 at 23:10
  • I want to use it to define the structure and set defaults and from there create instances of that structure....how did I break the code... –  Jul 30 '12 at 23:11
  • How does one create instance based object literals? –  Jul 30 '12 at 23:14
  • 1
    @HiroProtagonist: Change `var MC.o_p =` to `MC.o_p =`, you don't use `var` when specifying an object property. Remove `new` from the method call, as it's not a proper object constructor. You can put your code that creates an object in a function if you like, but there is no need to make that function a part of an object. – Guffa Jul 30 '12 at 23:17
  • ...I've done it all the wrong ways nows...is what I have now (update 3) correct? –  Jul 30 '12 at 23:19
  • @HiroProtagonist: Yes, that works. The `MC` object however serves no purpose for creating the object in the function. You can use a regular function to ge the same result. – Guffa Jul 30 '12 at 23:30
  • I use the MC "namespace" per "am not I am" to organize my code...you can't tell from the snippet but I do. –  Jul 30 '12 at 23:30