0

i think i did not understand javascript module pattern.

I just create this module:

var mycompany = {};
mycompany.mymodule = (function() {
    var my = {};
    var count = 0;

    my.init = function(value) {
        _setCount(value);
    }

    // private functions
    var _setCount = function(newValue) {
        count = newValue;
    }
    var _getCount = function() {
        return count;
    }

    my.incrementCount = function() {
        _setCount(_getCount() + 1);
    }
    my.degreeseCount = function() {
        _setCount(_getCount() - 1);
    }

    my.status = function() {
        return count;
    }

    return my;
})();


var a = mycompany.mymodule;
var b = mycompany.mymodule;
console.debug(a, 'A at beginning');
console.debug(a, 'B at beginning');
a.init(5);
b.init(2);
console.log('A: ' + a.status()); // return 2 (wtf!)
console.log('B: ' + b.status()); // return 2`

Where is the mistake? I thought that my code would have returned to me not 2 value, but 5.

What's the reason?

1
  • I think you have to use var a = new mycompany.mymodule. Else it just refrence to object. Commented Nov 5, 2012 at 9:52

3 Answers 3

2

a and b are the exact same objects.

var a = mycompany.mymodule;
var b = mycompany.mymodule;

What you want to do is create two different objects which have the same prototype. Something similar to this:

mycompany.mymodule = (function () { 
  var my = function () {};
  my.prototype.init = function (value) {
    _setCount(value);
  };
  my.prototype.incrementCount = ...
  // ...

  return my;
}());

a = new mycompany.mymodule();
b = new mycompany.mymodule();
a.init(5);
b.init(2);

For more info, research "javascript prototypal inheritance"

Sign up to request clarification or add additional context in comments.

Comments

1

In JavaScript, objects are passed by reference, not copied.

To explain further, here is a simplified version of your code:

var pkg = (function () {
  var x = {};
  return x;
}());

var a = pkg;
var b = pkg;

You do not create two separate objects but only reference the object pointed at by pkg from both a and b. a and b are exactly the same.

a === b // true

This means that calling a method on a you are ultimately doing the same to b (it points to the same object—x.)

You don't want to use the module pattern for this. You want the usual constructor+prototype.

function Pkg() {
  this.count = 0;
};
Pkg.prototype.init = function (count) { this.count = count; };

var a = new Pkg();
var b = new Pkg();

a === b // false

a.init(2);
a.count === 2 // true
b.count === 2 // false

2 Comments

thanks! can i make count a private variable? and for others private functions?
Why? Private in JS means "don't touch that" which we signal by using an underscore in front or behind the variable name. If you insist, you can have function Pkg() { this.count = 0; this.init = function () { ... }; } The variable count is then only visible to the constructor and the method init.
0

Here is a good read about module pattern.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.