Example of using Coffeescript classes and RequireJS (or Curljs or similar) for client side browser library

前端 未结 3 1212
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 17:38

We want to develop a browser (client side only) library using Coffeescript, and in particular, we tend to use the \"class\" capability of Coffeescript quite a bit, in addition t

3条回答
  •  無奈伤痛
    2021-01-30 18:24

    First off, if you're using RequireJS you're going to have a not-easy time returning multiple "things" from a define function. RequireJS uses AMD (!NOT! CommonJS) format "standards", which doesn't contain a module.exports object for exporting "stuff" but instead relies on return something.

    With that said, I'm not exactly sure what you're looking for here but having a class work with RequireJS is pretty easy. Something like this:

    define ['my/required/module'], (myModule) ->
        class MyOtherModule
            privateField = 0
    
            constructor: ->
            publicMethod: ->
    
        return MyOtherModule
    

    This can be used in a require/define function just like any other script. Take this example:

    require ['my/other/module'], (MyOtherModule) ->
        instance = new MyOtherModule()
    

    We can even use it with "extends"

    define ['my/other/module'], (MyOtherModule) ->
        class MyThirdModule extends MyOtherModule
            ...   
    

    Hopefully this helps!

提交回复
热议问题