异步的模块合并管理器-m3m4版本和jquery版本(原创)
m3m4版本
(function(w){
var loading={}
var models={}
function define(name,args){
loading[name]=true
models[name]=args
}
function require(name,callback){
var next=function(){
require(name,callback)
}
if(loading[name]&&models[name].length==1){
models[name]=models[name][0]()
loading[name]=false
}
//解析
if(loading[name]&&models[name].length>1){
var args=models[name]
var func=args.pop()
var len=args.length
var dataArr=[]
for(var i=0;i<args.length;i++){
var model=args[i]
require(model,function(data){
dataArr.push(data)
if(--len==0){
var sync=args.indexOf("$callback")
if(sync==-1){
models[name]=func.apply(this,dataArr)
loading[name]=false
next()
}else{
dataArr[sync]=function(back){
models[name]=back
loading[name]=false
next()
}
func.apply(this,dataArr)
}
}
})
}
}else{
callback(models[name])
}
}
w.m3m4=function(func){
func(define,require)
}
})(typeof window!="undefined"?window:global)
demo1
m3m4(function(define,require){
define("a",[function(a){
return "a"
}])
define("b",["a","$callback",function(a,callback){
console.log(a)
setTimeout(function(){
callback("b sync")
},1000)
}])
define("c",["$callback","a","b",function(callback,a,b){
console.log(b)
console.log(a)
setTimeout(function(){
callback("c sync")
},1000)
}])
require("c",function(c){
console.log(c)
})
})
jQuery版本
(function($){
var loading={}
var models={}
function define(name,args){
loading[name]=true
models[name]=args
}
function require(name,callback){
var next=function(){
require(name,callback)
}
if(loading[name]&&models[name].length==1){
models[name]=models[name][0]()
loading[name]=false
}
//解析
if(loading[name]&&models[name].length>1){
var args=models[name]
var func=args.pop()
var len=args.length
var dataArr=[]
for(var i=0;i<args.length;i++){
var model=args[i]
require(model,function(data){
dataArr.push(data)
if(--len==0){
var sync=args.indexOf("$callback")
if(sync==-1){
models[name]=func.apply(this,dataArr)
loading[name]=false
next()
}else{
dataArr[sync]=function(back){
models[name]=back
loading[name]=false
next()
}
func.apply(this,dataArr)
}
}
})
}
}else{
callback(models[name])
}
}
$.define=define
$.require=require
})($)
demo2
var define= $.define
var require= $.require
define("a",[function(a){
return "a"
}])
define("b",["a","$callback",function(a,callback){
console.log(a)
setTimeout(function(){
callback("b sync")
},1000)
}])
define("c",["$callback","a","b",function(callback,a,b){
console.log(b)
console.log(a)
setTimeout(function(){
callback("c sync")
},1000)
}])
require("c",function(c){
console.log(c)
})
异步的模块合并管理器-m3m4版本和jquery版本(原创)的更多相关文章
- 使用 NuGet 包管理器在 Visual Studio 中安装和管理包
https://docs.microsoft.com/zh-cn/nuget/consume-packages/install-use-packages-visual-studio 通过 Window ...
- javascript 模块依赖管理的本质
模块模式定义 模块是'javascript'的一种设计模式,它为函数定义一个包装函数,并且该包装函数的返回值与模块的API保持一致: function createModule() { functio ...
- 使用 Windows 包管理器 (winget) 安装 .Net
用户可以在 Windows 10 和 Windows 11 计算机上使用 winget 命令行工具来发现.安装.升级.删除和配置应用程序. 此工具是 Windows 程序包管理器服务的客户端接口. 以 ...
- [Web 前端] 使用yarn代替npm作为node.js的模块管理器
cp from : https://www.jianshu.com/p/bfe96f89da0e Fast, reliable, and secure dependency managemen ...
- 使用yarn代替npm作为node.js的模块管理器
使用yarn代替npm作为node.js的模块管理器 转 https://www.jianshu.com/p/bfe96f89da0e Fast, reliable, and secure d ...
- nodejs-npm模块管理器
JavaScript 标准参考教程(alpha) 草稿二:Node.js npm模块管理器 GitHub TOP npm模块管理器 来自<JavaScript 标准参考教程(alpha)> ...
- JS模块规范 前端模块管理器
一:JS模块规范(为了将js文件像java类一样被import和使用而定义为模块, 组织js文件,实现良好的文件层次结构.调用结构) A:CommonJS就是为JS的表现来制定规范,因为js没有模块的 ...
- 在SSIS 的 64 位版本中不支持 Excel 连接管理器
Microsoft sql server 2008 R2——> SQL SERVER Business Intelligence Development Studio 使用EXCEL数据源或目标 ...
- Webpack: 为Web开发而生的模块管理器[转]
Webpack: 为Web开发而生的模块管理器 原文地址:http://hanjianwei.com/2014/09/10/webpack-package-manager-for-web/ 10 Se ...
随机推荐
- getopt两个模块getopt 和gun_getopt 的异同
getopt的两个模块getopt和gun_getopt都可以接收参数,但是又有不同; 先看 getopt.getopt这个模块: import sys import getopt def main( ...
- rest-framework之序列化组件
一:django自带序列化组件 Django内置的serializers(把对象序列化成json字符串) from django.core import serializers def test(re ...
- Robot Framework - 基础关键字 BuiltIn 库(二)
本篇教程,我们继续接着上篇内容进行讲解,我们本节教程讲解的是Robot Framework 机器人框架中的变量中使用判断.字符串的拼接.Evaluate的用法.调用Python文件.条件分支语句.以及 ...
- 认识HttpContext.User
HttpContext.User,即IPrincipal .net源代码 namespace System.Security.Principal { /// <summary>Define ...
- The Three Models of ASP.NET MVC Apps
12 June 2012 by Dino Esposito by Dino Esposito We've inherited from the original MVC pattern a ra ...
- cmake笔记
注:cmake . 当前文件夹(一个点) cmake .. 父目录(两个点) 例子一 一个经典的C程序,如何用cmake来进行构建程序呢? //main.c #include <stdi ...
- poj3080 Blue Jeans(暴枚+kmp)
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- Newtonsoft.Json.Linq
var json = "{\"name\":\"ok1\",\"sex\":\"man\"}"; / ...
- Graphics 小记
1.切图 drowg.DrawImage(productImg1, new System.Drawing.Rectangle(30, 30, 300, 300), new System.Drawing ...
- Agreement has been updated--Edit Phone Number最便捷解决办法(不需要安全提示问题和双重认证)
2018年06月04日亲测有效: CSDN博客跳转网址: