admin管理员组

文章数量:1305084

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 :)

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 :)

Share Improve this question edited Jun 26, 2011 at 15:33 user310291 asked Jun 26, 2011 at 15:10 user310291user310291 38.2k88 gold badges294 silver badges518 bronze badges 5
  • Could you provide an example? – Felix Kling Commented Jun 26, 2011 at 15:11
  • I think there is one here stackoverflow./questions/5815757/… – user310291 Commented Jun 26, 2011 at 15:29
  • Are you talking about having something like, multiple instances of jQuery? – vol7ron Commented Jun 26, 2011 at 15:31
  • yes except it's not Jquery, it's for my OWN framework lib. – user310291 Commented Jun 26, 2011 at 15:33
  • Yes, I understood that, which is what was implied by something like – vol7ron Commented Jun 26, 2011 at 15:50
Add a ment  | 

4 Answers 4

Reset to default 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 plete 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.

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();

"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?

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 e 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.

本文标签: How to allow multiple instances in javascript if object wrapped in anonymous funcStack Overflow