requirejs源码分析: define 方法
define = function (name, deps, callback) {
var node, context;//Allow for anonymous modules
if (typeof name !== 'string') {
//Adjust args appropriately
callback = deps;
deps = name;
name = null;
}//This module may not have dependencies
if (!isArray(deps)) {
callback = deps;
deps = null;
}//If no name, and callback is a function, then figure out if it a
//CommonJS thing with dependencies.
if (!deps && isFunction(callback)) {
deps = [];
//Remove comments from the callback string,
//look for require calls, and pull them into the dependencies,
//but only if there are function args.
if (callback.length) {
callback
.toString()
.replace(commentRegExp, '')
.replace(cjsRequireRegExp, function (match, dep) {
deps.push(dep);
});//May be a CommonJS thing even without require calls, but still
//could use exports, and module. Avoid doing exports and module
//work though if it just needs require.
//REQUIRES the function to expect the CommonJS variables in the
//order listed below.
deps = (callback.length === 1 ? ['require'] : ['require', 'exports', 'module']).concat(deps);
}
}//If in IE 6-8 and hit an anonymous define() call, do the interactive
//work.
if (useInteractive) {
node = currentlyAddingScript || getInteractiveScript();
if (node) {
if (!name) {
name = node.getAttribute('data-requiremodule');
}
context = contexts[node.getAttribute('data-requirecontext')];
}
}//Always save off evaluating the def call until the script onload handler.
//This allows multiple modules to be in a file without prematurely
//tracing dependencies, and allows for anonymous module support,
//where the module name is not known until the script onload event
//occurs. If no context, use the global queue, and get it processed
//in the onscript load callback.
(context ? context.defQueue : globalDefQueue).push([name, deps, callback]);
};
这里的代码比较简单, 主要是把将是将前端的参数 添加到 队列 中。
第一个参数name, 可以自定义当前模块的名称, 用于require时可以使用这个名称进行加载。 现在很少使用这个参数,都使用路径的方法加载模块了。
requirejs源码分析: define 方法的更多相关文章
- requirejs源码分析: requirejs 方法–2. context.require(deps, callback, errback);
上一篇 requirejs源码分析: requirejs 方法–1. 主入口 中的return context.require(deps, callback, errback); 调用的是make ...
- requirejs源码分析: requirejs 方法–1. 主入口
该方法是 主要的入口点 也是最常用的方法. req = requirejs = function (deps, callback, errback, optional) { //Find the ri ...
- mybatis源码分析(方法调用过程)
十一月月底,宿舍楼失火啦,搞得20多天没有网,目测直到放假也不会来了... 正题 嗯~,其实阅读源码不是为了应付面试,更重要的让你知道,大师是怎样去写代码的,同样是用Java,为啥Clinton Be ...
- requirejs源码分析: 路径
1. 没有设置baseUrl(一般我们都会设置baseurl) 在没有设置baseUrl时, 默认 baseurl: "./" 当指定data-mai ...
- cocos2D-x demo 的源码分析 #define ..##.. 的妙用.
最近在看cocos2d-x 但不知道如何下手,于是先看一下他编译的完成的testcpp的源码.发现了下面一段程序 typedef CCLayer* (*NEWTESTFUNC)(); #define ...
- requirejs源码分析: config中shim
shim处理的源码: //Merge shim if (cfg.shim) { eachProp(cfg.shim, funct ...
- requirejs源码分析,使用注意要点
本文将深度剖析require.js代码,为了是大家更高效.正确的去使用它,本文不会介绍require的基本使用! 概要 先来一个流程图来概要一下大概流程 在require中,根据AMD(Asynchr ...
- WorkerMan源码分析(resetStd方法,PHP中STDIN, STDOUT, STDERR的重定向)
WorkerMan中work.php中 resetStd 方法中代码如下 public static function resetStd() { if (!static::$daemonize || ...
- requirejs源码分析
随机推荐
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- nginx configure参数
下面是nginx源码程序的configure参数: --prefix= 指向安装目录.默认为:/usr/local/nginx --sbin-path= 指定执行程序文件存放位置.默认为:prefix ...
- python模块学习之json
更多信息请参考官网地址: https://docs.python.org/3.6/library/json.html 19.2. json - JSON编码器和解码器 Source code: Lib ...
- 浅谈一致性hash
相信做过互联网应用的都知道,如何很好的做到横向扩展,其实是个蛮难的话题,缓存可横向扩展,如果采用简单的取模,余数方式的部署,基本是无法做到后期的扩展的,数据迁移及分布都是问题,举个例子: 假设采用取模 ...
- 开源码应用之Eclipse篇
开写这篇的时候,恰逢Eclpse Mars(4.5)正式公布,最终由日蚀变登火星了,也离我開始基于Eclipse开发产品已经过去10年,这10年间,经历了Eclipse由私有核心框架到拥抱OSGi, ...
- Android-X86 VirtualBox 安装安卓后的一些设置
可以用虚拟机设置双显卡,一个用于调试,一个用于连接外网 一个桥接一个host only 安卓Home键 -> Win键 安装返回键 -> ESC键 ALT + F1 调出管理员控制台 AL ...
- 目标跟踪之klt---光流跟踪法
近来在研究跟踪,跟踪的方法其实有很多,如粒子滤波(pf).meanshift跟踪,以及KLT跟踪或叫Lucas光流法,这些方法各自有各自的有点,对于粒子滤波而言,它能够比较好的在全局搜索到最优解,但其 ...
- echarts Y轴刻度保留几位小数
yAxis: [ { type: 'value', name: '雨量(mm)', nameLocation: 'start', inverse: true, axisLabel: { ...
- ios 2017启动页(Launch Screen Images)、图标(App Icon)尺寸大小
ios 2017启动页(Launch Screen Images).图标(App Icon)尺寸大小 iPhone Portrait iOS 8,9-Retina HD 5.5 (1242×220 ...
- HTML学习笔记——语法+骨架
一.什么是HTML HTML是用来制作网页的标记语言 HTML是Hypertext Markup Language的英文缩写,即超文本标记语言 HTML语言是一种标记语言,不需要编译,直接由浏览器执行 ...