1
我花了一些時間在我認爲是一個錯誤,直到我找到解決方法。對象鏈接審問
但我不明白爲什麼以前的代碼失敗。
請有洞察力嗎?
失敗代碼:
getModule: ->
Gmaps4Rails.Google
createMap : ->
new @getModule().Map()
工作代碼:
constructor:
@module = @getModule()
getModule: ->
Gmaps4Rails.Google
createMap : ->
new @module.Map()
我會理解,如果我寫了:'new this.getModule.Map()',但事實並非如此。這轉化爲:'返回新函數(){return Gmaps4Rails.Google; }()'我猜 – apneadiving 2012-08-14 19:39:28
@apneadiving - 本質上 - 它更接近於將其翻譯爲'var _temp = function(){return Gmaps4Rails.Google; };返回新的_temp();' – 2012-08-14 19:41:23
因此'這個'繼承'Gmaps4Rails.Google'然後this.Map()'被觸發?聽起來很瘋狂:)(+1在此期間) – apneadiving 2012-08-14 19:48:19