찾다

 >  Q&A  >  본문

javascript - 使用coffeescript如何编写模块加载器的逻辑判断

如下是一个jq-plugin的模块加载器预判,这样的代码如何用coffee写呢?

    (function (factory) {
        if (typeof define === 'function' && define.amd) {
            // AMD
            define(['jquery'], factory)
        } else if (typeof exports === 'object') {
            // CommonJS
            factory(require('jquery'))
        } else if (typeof define === 'function' && define.cmd) {
            // CMD
            define(function (require) {
                require('jquery')
                factory($)
            })
        } else {
            window.Validator = factory(jQuery)
        }
    }, (function ($) {
        // do something
    }))
阿神阿神2776일 전569

모든 응답(1)나는 대답할 것이다

  • PHP中文网

    PHP中文网2017-04-10 14:37:45

    解决了,如下即可

    do (factory) ->
      if typeof define is "function" and define.amd
    
        # AMD
        define ["jquery"], factory
      else if typeof exports is "object"
    
        # CommonJS
        factory require("jquery")
      else if typeof define is "function" and define.cmd
    
        # CMD
        define (require) ->
          require "jquery"
          factory $
          return
    
      else
        window.Validator = factory(jQuery)
      return
    
    factory = () ->
    

    회신하다
    0
  • 취소회신하다