I get a strange behaviour with Firefox when modifying a variable and displaying it (live demo here):
var MyModule = ( function() {
var currentPosition = {x : 1, y : 2, z : 3};
function changePosition() { currentPosition.x = 17; };
return { changePosition : changePosition,
currentPosition : currentPosition };
} )();
console.log(MyModule.currentPosition); // 17, 2, 3 instead of 1, 2, 3 !!
MyModule.changePosition();
console.log(MyModule.currentPosition); // 17, 2, 3
Why did this happen ? (Why does current.Position
give 17
before it was modified to 17
?)
More generally, how to get/set a variable in a Revealing Module Pattern?
Screenshot with Firefox:
It looks like Firefox holds a reference to the object and when you inspect it you see its modified state in both cases.
Next time you can set a breakpoint on that line and watch the variables there. In my experience that is more reliable. When you change the log statements into logging the value instead of the object it will work always like expected.
About the module pattern. You implemented it like it should :)
This is my no.1 source of javascript patterns. -> Module pattern