异步模块加载机制-amd - javascript 模块化的问题
大家讲道理
大家讲道理 2017-04-10 16:07:42
[JavaScript讨论组]
;(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        //AMD
        define(factory);
    } else if (typeof exports === 'object') {
        //Node, CommonJS之类的
        module.exports = factory();
    } else {
        root.resLoader = factory(root);
        // root 是window  window.resLoader = (function(){})(window) 这里的window有特殊含义么?还是只是一个匿名立即执行函数
    }
}(this, function () {
    var isFunc = function(f){
        return typeof f === 'function';
    }
    //构造器函数
    function resLoader(config){
        this.option = {
            resourceType : 'image', //资源类型,默认为图片
            baseUrl : './', //基准url
            resources : [], //资源路径数组
            onStart : null, //加载开始回调函数,传入参数total
            onProgress : null, //正在加载回调函数,传入参数currentIndex, total
            onComplete : null //加载完毕回调函数,传入参数total
        }
        if(config){
            for(i in config){
                this.option[i] = config[i];
            }
        }
        else{
            alert('参数错误!');
            return;
        }
        this.status = 0; //加载器的状态,0:未启动   1:正在加载   2:加载完毕
        this.total = this.option.resources.length || 0; //资源总数
        this.currentIndex = 0; //当前正在加载的资源索引
    };

    resLoader.prototype.start = function(){
        this.status = 1;
        var _this = this;
        var baseUrl = this.option.baseUrl;
        for(var i=0,l=this.option.resources.length; i

root.resLoader = factory(root);

 root 是window  window.resLoader = (function(){})(window) 
 这里的window有特殊含义么?还是只是一个匿名立即执行函数 
 还有就是这种支持amd commmon 的写法 有个模式名称么?
   
   
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(2)
巴扎黑

root 是window window.resLoader = (function(){})(window) 这里的window有特殊含义么?还是只是一个匿名立即执行函数 还有就是这种支持amd commmon 的写法 有个模式名称么?

巴扎黑

window是Window 对象
所有浏览器都支持 window 对象。它表示浏览器窗口。
所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员
全局变量是 window 对象的属性。
全局函数是 window 对象的方法。
甚至 HTML DOM 的 document 也是 window 对象的属性之一:

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号