开发者

How to allow multiple instances in javascript if object wrapped in anonymous func

As crockford and john resig advocated one should wrap object within anonymous function. But what if one needs to have multiple instances ? W开发者_如何学Pythonhen 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 :)


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜