javascript - 如何改写成兼容amd和普通方式调用?

查看:102
本文介绍了javascript - 如何改写成兼容amd和普通方式调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

define("utils.ClassUtil", function () {
    /**
     * @summary 寄生组合式继承
     * @param subType 子类
     * @param superType 超类
     * @memberof ClassUtil
     */
    function inheritPrototype(subType, superType) {
        var prototype = object(superType.prototype);
        prototype.constructor = subType;
        subType.prototype = prototype;
    }
    /**
     * @summary 返回构造函数
     * @memberof ClassUtil
     */
    function object(o) {
        function F () {}
        F.prototype = o;
        return new F();
    }


    return {
        inheritPrototype:inheritPrototype
    }
});

解决方案

(function (factory) {
    'use strict';
    if (typeof define === "function" && define.amd) {
        // AMD. Register as an anonymous module.
        define("utils.ClassUtil",[], factory);
    } else {
        // Browser globals
        factory(this.jQuery);
    }
}).call(this, function ($) {
    /**
     * @summary 寄生组合式继承
     * @param subType 子类
     * @param superType 超类
     * @memberof ClassUtil
     */
    function inheritPrototype(subType, superType) {
        var prototype = object(superType.prototype);
        prototype.constructor = subType;
        subType.prototype = prototype;
    }
    /**
     * @summary 返回构造函数
     * @memberof ClassUtil
     */
    function object(o) {
        function F() { }
        F.prototype = o;
        return new F();
    }
    var exports = {
        inheritPrototype: inheritPrototype
    }
    this.utils = this.utils || {};
    this.utils.ClassUtil = exports;
    return exports;
});

这篇关于javascript - 如何改写成兼容amd和普通方式调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆