3

It seems that using the following javascript module pattern, it is not possible to have mutually dependent modules:

var AA = (function(module, BB){
  module.sayHello = function(){
    alert("AA: hello!");
  };
  module.pokeYourFriend = function(){
    BB.sayHello();
  };
  return module;
})(AA || {}, BB);

var BB = (function(module, AA){
  module.sayHello = function(){
    alert("BB: hello!");
  };
  module.pokeYourFriend = function(){
    AA.sayHello();
  };
  return module;
})(BB || {}, AA);


> AA.pokeYourFriend();
*TypeError: Cannot call method 'sayHello' of undefined*

the above fails because BB does not exist at the time AA is created.

is there a pattern that allows this, or should mutual dependency be forbidden?

1 Answer 1

2

The RHS values of AA and BB are function expressions, which are evaluated in top-down order (which is the normal order in Javascript). Therefore, you cannot use BB before BB has been defined.

Instead of self-invoking, you can use a "constructor" that creates these modules and assigns a name to them ("AA", or "BB"):

var create = (function(name) {
   var _friend = null;
   var _name = name;

   return {
      sayHello: function() {
         alert(_name + ": hello!");
      },
      pokeYourFriend: function() {
         if(_friend != null) {
            _friend.sayHello();
         }
      },
      setFriend: function(friend) {
         _friend = friend;
      }
   };
});

var AA = create("AA");
var BB = create("BB");

AA.setFriend(BB);
BB.setFriend(AA);

AA.pokeYourFriend(); //alerts "BB: hello!"
BB.pokeYourFriend(); //alerts "AA: hello!"
Sign up to request clarification or add additional context in comments.

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.