I have a very strange problem with javascript. If you take the following code and run it, it will work fine without any errors, but if you comment on the first alert, it will throw an error on line 5 ( var _board = Bomber.BoardFactory.getBoard();), because BoardFactory does not exist (remember that everything worked without errors with the first warning) . I was able to reproduce this exact behavior with Firefox and Chrome.
Bomber = {};
Bomber.Game = function () {
var self = {};
var _board = Bomber.BoardFactory.getBoard();
self.init = function () {};
self.start = function () {};
return self;
}
alert("2");
(function () {
var instance;
Bomber.BoardFactory = {};
Bomber.BoardFactory.getBoard = function () {
if (!instance)
instance = new Bomber.Board();
return instance;
};
})();
alert("3");
Bomber.Board = function () {
var self = {};
return self;
}
$(document).ready(function () {
var game = Bomber.Game();
game.init();
game.start();
});
My question is, what could lead to this strange behavior? How is it possible that an alert call recognizes Bomber.BoardFactory?
source
share