mardi 6 septembre 2016

How to instantiate from a module as if it were a class with Module Pattern?

It's my understanding the Javascript Module Pattern simulates classes found in class-based languages, but I haven't been able to find an example of an object being instantiated using this pattern. In Java, classes are instantiated like this:

var myClassInstance = new MyClass();

All the Module Pattern tutorials I've seen explain the creation of a module with this basic syntax:

var MyModule = (function(){
    var privateMember = 'foo';

    function publicMember() {
    }        

    return {
        publicMember: publicMember
    }
}());

  1. How would I then create a new instance of MyModule in this example?
  2. Are modules meant to be instantiated?
  3. What's the proper way to instantiate an object using the Module Pattern?

Aucun commentaire:

Enregistrer un commentaire