上一篇 requirejs源码分析: requirejs 方法–1. 主入口  中的return context.require(deps, callback, errback);  调用的是makeRequire方法中的localRequire

 

function localRequire(deps, callback, errback) {
    var id, map, requireMod;

    if (options.enableBuildCallback && callback && isFunction(callback)) {
        callback.__requireJsBuild = true;
    }

    if (typeof deps === 'string') {
        if (isFunction(callback)) {
            //Invalid call
            return onError(makeError('requireargs', 'Invalid require call'), errback);
        }

        //If require|exports|module are requested, get the
        //value for them from the special handlers. Caveat:
        //this only works while module is being defined.
        if (relMap && hasProp(handlers, deps)) {
            return handlers[deps](registry[relMap.id]);
        }

        //Synchronous access to one module. If require.get is
        //available (as in the Node adapter), prefer that.
        if (req.get) {
            return req.get(context, deps, relMap, localRequire);
        }

        //Normalize module name, if it contains . or ..
        map = makeModuleMap(deps, relMap, false, true);
        id = map.id;

        if (!hasProp(defined, id)) {
            return onError(makeError('notloaded', 'Module name "' +
                        id +
                        '" has not been loaded yet for context: ' +
                        contextName +
                        (relMap ? '' : '. Use require([])')));
        }
        return defined[id];
    }

    //Grab defines waiting in the global queue.
    intakeDefines();

   //Mark all the dependencies as needing to be loaded.
    context.nextTick(function () {
        //Some defines could have been added since the
        //require call, collect them.
        intakeDefines();

        //实例化Module对象                   
        requireMod = getModule(makeModuleMap(null, relMap));

        //Store if map config should be applied to this require
        //call for dependencies.
        requireMod.skipMap = options.skipMap;

        //初始化 Module 对象
        requireMod.init(deps, callback, errback, {
            enabled: true
        });

        checkLoaded();
    });

    return localRequire;
}

这里主要是方法:

context.nextTick(function () {
        //Some defines could have been added since the
        //require call, collect them.
        intakeDefines();

        //实例化Module对象                   
        requireMod = getModule(makeModuleMap(null, relMap));

        //Store if map config should be applied to this require
        //call for dependencies.
        requireMod.skipMap = options.skipMap;

        //初始化 Module 对象
        requireMod.init(deps, callback, errback, {
            enabled: true
        });

        checkLoaded();
    });

这里使用context.nextTick是延迟4ms再执行(异步)。 这里不能很理解作者的目的。 是为了不卡主线程?

该方法主要流程:
1. 实例化Module。

requireMod = getModule(makeModuleMap(null, relMap));

        Module也是上下文newContext里的对象。这个是核心模块。包含以下主要的属性。

this.events = getOwn(undefEvents, map.id) || {};
this.map = map;
this.shim = getOwn(config.shim, map.id);
this.depExports = [];
this.depMaps = [];
this.depMatched = [];
this.pluginMaps = {};
this.depCount = 0;

该对象是封装模块,存在模块信息(shim,依赖等。)。 还包含对模块的加载,依赖加载,通知依赖等。

requirejs源码分析: requirejs 方法–2. context.require(deps, callback, errback);的更多相关文章

  1. requirejs源码分析: requirejs 方法–1. 主入口

    该方法是 主要的入口点 也是最常用的方法. req = requirejs = function (deps, callback, errback, optional) { //Find the ri ...

  2. requirejs源码分析: define 方法

    define = function (name, deps, callback) {     var node, context; //Allow for anonymous modules     ...

  3. requirejs源码分析: 路径

    1. 没有设置baseUrl(一般我们都会设置baseurl)        在没有设置baseUrl时, 默认  baseurl: "./"        当指定data-mai ...

  4. requirejs源码分析: config中shim

    shim处理的源码: //Merge shim                 if (cfg.shim) {                     eachProp(cfg.shim, funct ...

  5. requirejs源码分析,使用注意要点

    本文将深度剖析require.js代码,为了是大家更高效.正确的去使用它,本文不会介绍require的基本使用! 概要 先来一个流程图来概要一下大概流程 在require中,根据AMD(Asynchr ...

  6. mybatis源码分析(方法调用过程)

    十一月月底,宿舍楼失火啦,搞得20多天没有网,目测直到放假也不会来了... 正题 嗯~,其实阅读源码不是为了应付面试,更重要的让你知道,大师是怎样去写代码的,同样是用Java,为啥Clinton Be ...

  7. requirejs源码分析

  8. WorkerMan源码分析(resetStd方法,PHP中STDIN, STDOUT, STDERR的重定向)

    WorkerMan中work.php中 resetStd 方法中代码如下 public static function resetStd() { if (!static::$daemonize || ...

  9. 【requireJS源码学习01】了解整个requireJS的结构

    前言 现在工作中基本离不开requireJS这种模块管理工具了,之前一直在用,但是对其原理不甚熟悉,整两天我们来试着学习其源码,而后在探寻其背后的AMD思想吧 于是今天的目标是熟悉requireJS整 ...

随机推荐

  1. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  2. c++ using Handle Class Pattern to accomplish implementation hiding

    Reference material: Thinking In C++ 2nd eidition chapter 5 section "Handle classes" If the ...

  3. MFC 改变控件的大小和位置

    mfc 改变控件大小和位置用到的函数: ) void MoveWindow(int x, int y, int nWidth, int nHeight); ) void MoveWindow(LPCR ...

  4. makefile变量定义应用到c语言

    makefile是为组织程序工程的,其定义的宏怎样应用到c程序中呢? 我们知道Makefile中可定义变量或导出变量,make命令可定义变量:编译器(如gcc)可通过CFLAGS定义宏. 但如何才能使 ...

  5. ffmpeg 错误系统

     avcodec_decode_video2. returns -1094995529. #define AVERROR_INVALIDDATA FFERRTAG( 'I','N','D','A') ...

  6. Spectral Graph Theory的一些定理

    邻接矩阵的特征值和特征向量不会随着节点的排列不同而变化.两个图同构可以推出他们的邻接矩阵具有相同的特征值和特征向量,但是反过来不行.

  7. Python Theano ValueError: y_i value out of bounds

    参考 https://groups.google.com/forum/#!topic/theano-users/tY3fNAPYd9k 这个问题是由于outs的数量没有设置对. 里面写到 “excep ...

  8. 自己动手写ORM(01):解析表达式树生成Sql碎片

     在EF中,我们查询数据时可能会用拉姆达表达式 Where(Func<T,ture> func)这个方法来筛选数据,例如,我们定义一个User实体类 public class User { ...

  9. Unity3D学习笔记——NGUI之UIScrollView

    前言:有的时候Panel会被截取,里面的内容就不能显示完整,所以需要为其添加 scroll view组件. 一:将Panel放进一个scroll view只需要简单的3步: 1.首先在UI Root下 ...

  10. CFindReplaceDialog学习

    The CFindReplaceDialog class allows you to implement standard string Find/Replace dialog boxes in yo ...