[英]How can I extend google.maps.Map with coffeescript
我已經嘗試過標准方式:
class MyMap extends google.maps.Map
constructor: (mapDiv, opts)->
super(mapDiv, opts)
但是在這種情況下,占位符為空。
此問題是CoffeeScript制作類的方式與Google Maps Javascript API的編寫/混淆/最小化方式的組合。
CoffeeScript擴展類時,將創建類似於以下內容的代碼:
customnamespace.CustomMap = (function(_super) {
// '__extends' is a method that gets output at the
// top of every CoffeeScript compiled file
__extends(CustomMap, _super);
function CustomMap(mapDiv, opts) {
CustomMap.__super__.constructor.apply(this, arguments);
}
return CustomMap;
})(google.maps.Map);
在大多數情況下,尤其是在用CoffeeScript編寫“擴展名”的情況下,這非常有用。
但是在google.map.Maps的情況下(我懷疑)正在進行大范圍的范圍操作,這有點是撤銷CoffeeScript嘗試設置的范圍。 誠然,這是一個猜測。
因此,在這種情況下,是時候戴上舊的JavaScript帽子,並在構造函數上執行一些簡單的舊范圍鎖定了。 因此,刪除您的super
並apply
一行JavaScript將函數apply
到類的范圍中。 CoffeeScript只會揮舞,微笑並按原樣輸出JavaScript行。
class MyMap extends google.maps.Map
constructor: (mapDiv, opts)->
google.maps.Map.apply(this, [mapDiv, opts]);
說得通?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.