自调用执行函数,怎么使用AMD规范写成一个模块?

发布于 2022-09-04 23:55:05 字数 387 浏览 23 评论 0

   var myservice = (function() {
    var me = this;
    me.reportInfo = {};
    this.setReportInfo = function(data){
       me.reportInfo = data;
    };
    this.getReportInfo = function(){
       return me.reportInfo;
      };

    return me;
   })();

怎么将这一个js文件的内容定义为AMD规范的模块化,就像jQuery,我直接在require.config()引入,在其他多个不同的js文件中直接就可以使用myservice这个调用里面的任意一个方法。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舂唻埖巳落 2022-09-11 23:55:05

题主你都说jQuery了,参考一下jQuery的实现?

摘自jQuery 2.0源码片段

// Register as a named AMD module, since jQuery can be concatenated with other
    // files that may use define, but not via a proper concatenation script that
    // understands anonymous AMD modules. A named AMD is safest and most robust
    // way to register. Lowercase jquery is used because AMD module names are
    // derived from file names, and jQuery is normally delivered in a lowercase
    // file name. Do this after creating the global so that if an AMD module wants
    // to call noConflict to hide this version of jQuery, it will work.
    if ( typeof define === "function" && define.amd ) {
        define( "jquery", [], function () { return jQuery; } );
    }
var myservice = (function() {
    var me = this;
    me.reportInfo = {};
    this.setReportInfo = function(data){
       me.reportInfo = data;
    };
    this.getReportInfo = function(){
       return me.reportInfo;
      };

    return me;
   })();
if ( typeof define === "function" && define.amd ) {
        define( "myService", [], function () { 
            return myservice;
         } );
    }

这样?

作妖 2022-09-11 23:55:05

参考webuploader这个组件,整个组件都是使用AMD组织起来的,http://fex.baidu.com/webuploa...

韶华倾负 2022-09-11 23:55:05

UMD 摘自不知道啥地方了

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['jquery'], factory);
    } else if (typeof exports === 'object') {
        // Node, CommonJS之类的
        module.exports = factory(require('jquery'));
    } else {
        // 浏览器全局变量(root 即 window)
        root.returnExports = factory(root.jQuery);
    }
}(this, function ($) {
    //    方法
    function myFunc(){};
 
    //    暴露公共方法
    return myFunc;
}));

// 复杂实例
(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['jquery', 'underscore'], factory);
    } else if (typeof exports === 'object') {
        // Node, CommonJS之类的
        module.exports = factory(require('jquery'), require('underscore'));
    } else {
        // 浏览器全局变量(root 即 window)
        root.returnExports = factory(root.jQuery, root._);
    }
}(this, function ($, _) {
    //    方法
    function a(){};    //    私有方法,因为它没被返回 (见下面)
    function b(){};    //    公共方法,因为被返回了
    function c(){};    //    公共方法,因为被返回了
 
    //    暴露公共方法
    return {
        b: b,
        c: c
    }
}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文