上一篇 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. HttpClient4.X 升级 入门 + http连接池使用

    转载请注明出处,谢谢~ http://blog.csdn.net/shootyou/archive/2011/05/12/6415248.aspx 在一次服务器异常的排查过程当中(服务器异常排查的过程 ...

  2. Step By Step(Lua调用C函数)

    原文: http://www.cnblogs.com/stephen-liu74/archive/2012/07/23/2469902.html Lua可以调用C函数的能力将极大的提高Lua的可扩展性 ...

  3. StoryBoard不使用AutoLayout情况下 按比例快速兼容适配iPhone6/6 Plus教程【转载】

    StoryBoard不使用AutoLayout情况下 按比例快速兼容适配iPhone6/6 Plus教程[转] 声明:本文章是为了后期快速兼容6和6Plus的按比例放大方法,对于部分读者来说可能觉得该 ...

  4. (初学者)安装hadoop集群注意事项

    1.关闭防火墙 2.所有的hadoop操作都是hadoop用户下面的,同时需要用hadoop用户登录之后,对于其他的机器的hadoop用户可以免密登录 3.hadoop用户在root组下面,不是附加组 ...

  5. uva753 A Plug for UNIX 网络流最大流

    C - A Plug for UNIX    You are in charge of setting up the press room for the inaugural meeting of t ...

  6. 在 RHEL/CentOS 7 上配置NTP时间服务器

    一.NTP简介 网络时间协议 - NTP - 是运行在传输层 123 号端口的 UDP 协议,它允许计算机通过网络同步准确时间.随着时间的流逝,计算机内部时间会出现漂移,这会导致时间不一致问题,尤其是 ...

  7. 第一百九十五节,jQuery EasyUI,Resizable(调整大小)组件

    jQuery EasyUI,Resizable(调整大小)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Resizeable(调整大小)组 ...

  8. C++11写算法之顺序查找

    从这篇博文起,将尝试使用C++11来写常用算法与数据结构. 本篇博文以最简单的顺序查找作为系列博文的起点,并作约定如下: 1,变量名 : varList : 函数名 : SequentialFind ...

  9. OpenCV学习笔记十三:opencv_videostab模块

    一,简介: 该库用于视频稳像.

  10. CodeForces 450A 队列

    Description There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let ...