原因是这new anonymous function与new Gmaps4Rails.Google()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();
}
};
问题是return new this.getModule().Map();转换为return new function() { return Gmaps4Rails.Google; }-忽略返回值并使用this(这是从匿名函数继承的新对象)。因此,该行本质上转换为return {}.Map();由于对象没有Map方法,因此您会收到错误。
当您设置@module为Gmaps4Rails.Googlethen 的引用时,当您调用时,new @module.Map()您实际上是在调用new Gmaps4Rails.Google- 它返回一个具有Map方法的对象 - 因此一切正常。