I create a Class using module pattern cuz I need some private members and functions. The problem is I can't create multiple instance for one module. If I create a new instance, it will replace all the instances I created before.
Here is the code.
var MyObj = (function() {
var myVar;
function MyObj(arg) {
myVar = arg;
}
MyObj.prototype.print = function() {
console.log(myVar);
};
return MyObj;
})();
var instance1 = new MyObj('instance1');
var instance2 = new MyObj('instance2');
instance1.print(); // instance2
instance2.print(); // instance2
Here is my questions: 1. Does this mean i can't create multiple instance for one Class if i want to use this pattern? 2. If i can't use this pattern, is there anyway else can have private in Class?