Skip to main content
added 19 characters in body
Source Link
Dagg
  • 4.5k
  • 1
  • 24
  • 41

There's nothing really wrong with variation 1, but there's also no point in putting things in pr. Sure you can chain it, but why chain something where this only refers to half the stuff you need (the private stuff)? You might as well just use function declarations. And you can get rid of PU, since it's kind of a meaningless variable name:

var MyModule = (function(){
    function fetch(obj){}; // fetching data
    function build(obj){}; // building DOM of module
    function show(obj){};  // showing our object
    function hide(obj){};  // hiding our object
     //... Other private methods
     return {
         // Public methods
         init: function(){
             show(build(fetch(this)));
             return this; // for making chains
         },
         update: function(){}
         // ... other Public methods
     };
})();

The problem with variation two is that you create those functions whenever the constructor is called (so, more than once).

The normal way to avoid this is to do something like

var MyModule = (function(){

    function fetch(obj){}; // fetching data
    function build(obj){}; // building DOM of module
    function show(obj){};  // showing our object
    function hide(obj){};  // hiding our object

    function MyModule(){ 
        // constructor code
    };

    MyModule.prototype.init = function(){
        fetchshow().build().showfetch(this)));
        return this;
    };  

    MyModule.prototype.update = function(){};
    //... other Public methods

    return MyModule;

}());

Does that make sense?

This is not really a "module" anymore, by the way, just a normal constructor with some function properties attached to the prototype. But it looks like that's what you want, since you need multiple instances of it.

The problem with variation two is that you create those functions whenever the constructor is called (so, more than once).

The normal way to avoid this is to do something like

var MyModule = (function(){

    function fetch(){}; // fetching data
    function build(){}; // building DOM of module
    function show(){};  // showing our object
    function hide(){};  // hiding our object

    function MyModule(){ 
        // constructor code
    };

    MyModule.prototype.init = function(){
        fetch().build().show();
        return this;
    };  

    MyModule.prototype.update = function(){};
    //... other Public methods

    return MyModule;

}());

Does that make sense?

This is not really a "module" anymore, by the way, just a normal constructor with some function properties attached to the prototype. But it looks like that's what you want, since you need multiple instances of it.

There's nothing really wrong with variation 1, but there's also no point in putting things in pr. Sure you can chain it, but why chain something where this only refers to half the stuff you need (the private stuff)? You might as well just use function declarations. And you can get rid of PU, since it's kind of a meaningless variable name:

var MyModule = (function(){
    function fetch(obj){}; // fetching data
    function build(obj){}; // building DOM of module
    function show(obj){};  // showing our object
    function hide(obj){};  // hiding our object
     //... Other private methods
     return {
         // Public methods
         init: function(){
             show(build(fetch(this)));
             return this; // for making chains
         },
         update: function(){}
         // ... other Public methods
     };
})();

The problem with variation two is that you create those functions whenever the constructor is called (so, more than once).

The normal way to avoid this is to do something like

var MyModule = (function(){

    function fetch(obj){}; // fetching data
    function build(obj){}; // building DOM of module
    function show(obj){};  // showing our object
    function hide(obj){};  // hiding our object

    function MyModule(){ 
        // constructor code
    };

    MyModule.prototype.init = function(){
        show(build(fetch(this)));
        return this;
    };  

    MyModule.prototype.update = function(){};
    //... other Public methods

    return MyModule;

}());

Does that make sense?

This is not really a "module" anymore, by the way, just a normal constructor with some function properties attached to the prototype. But it looks like that's what you want, since you need multiple instances of it.

added 19 characters in body
Source Link
Dagg
  • 4.5k
  • 1
  • 24
  • 41

The problem with variation two is that you create those functions whenever the constructor is called (so, more than once).

The normal way to avoid this is to do something like

var MyModule = (function(){

    function fetch(){}; // fetching data
    function build(){}; // building DOM of module
    function show(){};  // showing our object
    function hide(){};  // hiding our object

    function MyModule(){ 
        // constructor code
    };

    MyModule.prototype.init = function(){
        fetch().build().show();
        return this;
    };  

    MyModule.prototype.update = function(){};
    //... other Public methods

    return MyModule;

}());

Does that make sense?

This is not really a "module" anymore, by the way, just a normal constructor with some function properties attached to the prototype. But it looks like that's what you want, since you need multiple instances of it.

The problem is that you create those functions whenever the constructor is called (so, more than once).

The normal way to avoid this is to do something like

var MyModule = (function(){

    function fetch(){}; // fetching data
    function build(){}; // building DOM of module
    function show(){};  // showing our object
    function hide(){};  // hiding our object

    function MyModule(){ 
        // constructor code
    };

    MyModule.prototype.init = function(){
        fetch().build().show();
        return this;
    };  

    MyModule.prototype.update = function(){};
    //... other Public methods

    return MyModule;

}());

Does that make sense?

This is not really a "module" anymore, by the way, just a normal constructor with some function properties attached to the prototype. But it looks like that's what you want, since you need multiple instances of it.

The problem with variation two is that you create those functions whenever the constructor is called (so, more than once).

The normal way to avoid this is to do something like

var MyModule = (function(){

    function fetch(){}; // fetching data
    function build(){}; // building DOM of module
    function show(){};  // showing our object
    function hide(){};  // hiding our object

    function MyModule(){ 
        // constructor code
    };

    MyModule.prototype.init = function(){
        fetch().build().show();
        return this;
    };  

    MyModule.prototype.update = function(){};
    //... other Public methods

    return MyModule;

}());

Does that make sense?

This is not really a "module" anymore, by the way, just a normal constructor with some function properties attached to the prototype. But it looks like that's what you want, since you need multiple instances of it.

Source Link
Dagg
  • 4.5k
  • 1
  • 24
  • 41

The problem is that you create those functions whenever the constructor is called (so, more than once).

The normal way to avoid this is to do something like

var MyModule = (function(){

    function fetch(){}; // fetching data
    function build(){}; // building DOM of module
    function show(){};  // showing our object
    function hide(){};  // hiding our object

    function MyModule(){ 
        // constructor code
    };

    MyModule.prototype.init = function(){
        fetch().build().show();
        return this;
    };  

    MyModule.prototype.update = function(){};
    //... other Public methods

    return MyModule;

}());

Does that make sense?

This is not really a "module" anymore, by the way, just a normal constructor with some function properties attached to the prototype. But it looks like that's what you want, since you need multiple instances of it.