javascript - 使用coffeescript如何编写模块加载器的逻辑判断
阿神
阿神 2017-04-10 14:35:45
0
1
565

如下是一个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
    }))
阿神
阿神

闭关修行中......

reply all(1)
洪涛

解决了,如下即可

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 = () ->
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template