The reason is that new anonymous function is not the same as new Gmaps4Rails.Google() in JavaScript.
// Translated JavaScript code (simplified):
var your_module = {
getModule: function() {
return Gmaps4Rails.Google;
},
createMap: function() {
// This is where things go wrong
return new this.getModule().Map();
}
};
The problem is that return new this.getModule().Map(); translates to return new function() { return Gmaps4Rails.Google; } - which ignores the return value and uses this (which is a new object inheriting from the anonymous function). Thus the line essentially translates to return {}.Map(); Since objects do not have a Map method, you get an error.
When you set @module to be a reference to Gmaps4Rails.Google then when you call new @module.Map() you are actually calling new Gmaps4Rails.Google - and it returns an object which has a Map method - thus everything works.