3

As crockford and john resig advocated one should wrap object within anonymous function. But what if one needs to have multiple instances ? When it is wrapped only one instance will exist (singleton).

Update: I forget to say I'm of course talking about the root object of a framework (not jquery but my own) not of any object. Since it's a framework the number of instances is not known and decided by client.

I guess 99.99% of people just use frameworks and don't build their own so if you don't know don't try to answer something you do not understand yourself :)

user310291
  • 36,946
  • 82
  • 271
  • 487

4 Answers4

6

This is [a simple example of] a way to create a pseudo namespace (there are more ways) from a directly instantiated function:

var NS = (function(){
   function Person(name,gender,age){
     this.name = name || '';
     this.gender = gender || '';
     this.age = age || 0;
   }

   return {
     createPerson: function(n,g,a) {return new Person(n,g,a);}
   };

}());

Now NS is the pseudo namespace from which you can create a Person instance like this:

var pete = NS.createPerson('Pete','male',23);
alert(pete.name); //=> Pete

Within the NS function you can create a complete framework using functions, objects, local variables etc. In the returning object you can include all the public methods you need to make it all ticking.

KooiInc
  • 119,216
  • 31
  • 141
  • 177
2

Are you looking for something like this?

var autos = {

    Car: function (id, model) {
        this.id = id;
        this.model = model;
        this.isMoving = false;

        this.drive = function drive() {
            this.isMoving = true;
        };

        this.stop = function stop() {
            this.isMoving = false;
        };
    }

};

Usage:

var car = new autos.Car(123, 'MDX');
car.drive();
car.stop();
Kon
  • 27,113
  • 11
  • 60
  • 86
  • autos is not wrapped by an anonymous function. when wrapped you cannot make new that's my point :) – user310291 Jun 26 '11 at 16:11
  • @user310291: from everything you've said, it seems like you're trying to access the scope of a closure inside the global space, which by design will never work. The whole point of having an anonymous function is to keep it separate from the global space. – vol7ron Jun 26 '11 at 19:20
  • How in the hell do you expect to instantiate an anonymous anything? That doesn't make any sense. What am I missing? – Kon Jun 26 '11 at 19:32
1

"As crockford and john resig advocated one should wrap object within anonymous function."

Yes, specifically to avoid leaking objects into the global space... but I don't think they suggest doing this everywhere you use objects at all.

If you want multiple objects in the global space, how will you name them? CarOne and CarTwo?

How about, instead, you use a single global called MyGarage, and MyGarage can contain CarOne and CarTwo?

Steve
  • 31,144
  • 19
  • 99
  • 122
  • OK I forget to say I'm of course talking about the root object of a framework not of any object. – user310291 Jun 26 '11 at 15:27
  • Since it's a framework the number of instances is not known and decided by client. – user310291 Jun 26 '11 at 15:32
  • You keep throwing around the term framework and then using instances. You need a better example. – vol7ron Jun 26 '11 at 15:33
  • A framework is implemented with a root object so why do you contrast the two ? – user310291 Jun 26 '11 at 15:35
  • 1
    I contrast the two, because the framework shouldn't need an instance; instances should be inside the framework. By design a framework is meant to be a tool that assists instances, by having multiple you will just be using unnecessary resources. Again, if you have some sort of an odd situation that contrasts this idea (and I'm not saying you don't), you need to show a better example. – vol7ron Jun 26 '11 at 15:49
  • Couldn't agree more with vol7ron. (well, you do need _one_ instance... but that's it). – Steve Jun 27 '11 at 04:39
1

2011-06-26 (1:26P EST) You may be looking for a deep copy of your framework


Since you haven't provided an example, here is one using jQuery. For each new "instance" you would add a property to frameworks, which would hold it. Or you could just use a frameworks array and keep pushing your instances onto the stack. Either way, it will be one variable to hold the multiple instances/copies.

var frameworks = {};
frameworks.a = jQuery.extend(true, {}, jQuery);
frameworks.b = jQuery.extend(true, {}, jQuery);


(function($){$.fn.foo = function(arg){var foo=arg;}})(frameworks.a);
console.log(frameworks.a.fn.foo.toString());                  // uses 'arg'

(function($){$.fn.foo = function(bar){var foo=bar;}})(frameworks.b);
console.log((frameworks.a).fn.foo.toString());                // still uses 'arg'


Original Answer


Is this something that you're after?

var frameworks = {};
frameworks.base = function(i){var foo = i; return foo;};
frameworks.a = (frameworks.base)('a');
frameworks.b = (frameworks.base)('b');

document.write(frameworks.a);
document.write(frameworks.b);

Author's Comment: I guess 99.99% of people just use frameworks and don't build their own so if you don't know don't try to answer something you do not understand yourself :)

Response: I guess 99.99% of the people that do build their own, don't come across something like this, because they understand the purpose of a framework and that if you need instances of one, you have a design flaw. Additionally, if someone was so "well equipped" to design a framework that doesn't already exist in the www, then they should be able to understand how to implement their own "instances" w/o convoluting the global namespace.

vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • Really ? Then you didn't read John Resig, he's talking about multiple instances. – user310291 Jun 26 '11 at 16:03
  • You didn't wrap your framework in anonymous function that's my very point in my question. – user310291 Jun 26 '11 at 16:05
  • no, the framework variable is a variable that holds the various framework instances (eg a,b). You need some way of calling on those frameworks in the global scope, much like you use `$` or `jQuery` to call on that framework. When it comes to John Resig, he isn't the end-all-be-all of everything JavaScript, after all he is from Boston. – vol7ron Jun 26 '11 at 16:49