【问题标题】:How can I extend google.maps.Map with coffeescript如何使用咖啡脚本扩展 google.maps.Map
【发布时间】:2012-11-17 07:15:14
【问题描述】:

我已经尝试过标准方式:

class MyMap extends google.maps.Map
  constructor: (mapDiv, opts)->
    super(mapDiv, opts)

但在这种情况下占位符是空的。

【问题讨论】:

    标签: javascript google-maps-api-3 coffeescript extends


    【解决方案1】:

    这个问题是 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);
    

    在大多数情况下,尤其是在“extendee”是用 CoffeeScript 编写的情况下,这很有效。

    但在 google.map.Maps 的情况下,(我怀疑)有一大堆范围操作正在进行,它有点撤销 CoffeeScript 试图设置的范围。诚然,这是一个猜测。

    所以在这种情况下,是时候戴上你的 JavaScript 帽子,对构造函数做一些简单的老式作用域锁定了。因此,使用一行 JavaScript 将您的 superapply 函数放在类的范围内。 CoffeeScript 只会挥手、微笑并按原样输出 JavaScript 行。

    class MyMap extends google.maps.Map
        constructor: (mapDiv, opts)->
            google.maps.Map.apply(this, [mapDiv, opts]);
    

    有意义吗?

    【讨论】:

    • 谢谢,我使用了类似的解决方案:google.maps.Map.call(this, $('#map_canvas')[0], opts)
    猜你喜欢
    • 2014-04-10
    • 2013-10-30
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多