使用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
    }))

coffeescript JavaScript

狂気DNEET 10 years, 3 months ago

解决了,如下即可


 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 = () ->

火炎之纹章 answered 10 years, 3 months ago

Your Answer