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 方法的更多相关文章

  1. requirejs源码分析: requirejs 方法–2. context.require(deps, callback, errback);

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

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

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

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

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

  4. requirejs源码分析: 路径

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

  5. cocos2D-x demo 的源码分析 #define ..##.. 的妙用.

    最近在看cocos2d-x 但不知道如何下手,于是先看一下他编译的完成的testcpp的源码.发现了下面一段程序 typedef CCLayer* (*NEWTESTFUNC)(); #define ...

  6. requirejs源码分析: config中shim

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

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

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

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

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

  9. requirejs源码分析

随机推荐

  1. nginx configure参数

    下面是nginx源码程序的configure参数: --prefix= 指向安装目录.默认为:/usr/local/nginx --sbin-path= 指定执行程序文件存放位置.默认为:prefix ...

  2. c++ how to make your own class a valid key type for std::map?

    In Java, if you want your own class to be a valid key type of the container, you just need to make i ...

  3. 如何在GitHub上删除某个文件夹?

    步骤: (以删除.idea文件夹为例) git rm -r --cached .idea #--cached不会把本地的.idea删除 git commit -m 'delete .idea dir' ...

  4. Vim使用技巧(4) -- 命令行模式 【持续更新】

    基本保存,退出,帮助 :help //帮助 :w //保存 :q //退出 :wq //保存后退出 :q! //强制不保存退出 %s/a/b/g //将当前文件的a全部替换成b /abc //正向查找 ...

  5. tomcat的bin目录中startup.bat/tomcat.6.exe/tomcat6w.exe区别

    一.tomcat6.exe 与 startup.bat的区别 1.两者都可以用于启动Tomcat tomcat6.exe则是必须将tomcat注册Windows服务之后才可以用于启动tomcat服务; ...

  6. Hibernate每个层次类一张表(使用注释)

    在上一文章中,我们使用xml文件将继承层次映射到一个表. 在这里,我们将使用注释来执行同样的任务.需要使用@Inheritance(strategy = InheritanceType.SINGLE_ ...

  7. 实体框架迁移,EntityFramework

    主要就是这个:http://msdn.microsoft.com/zh-cn/data/jj591621.aspx 下面这个是写得不错的,比较详细: 首先打开工具--->>>库程序包 ...

  8. ROS导航之地图costmap_2d与bresenham算法

    读者可以参读http://wiki.ros.org/costmap_2d   costmap_2d: 无论是激光雷达还是如kinect 或xtion pro深度相机作为传感器跑出的2D或3D SLAM ...

  9. C# 关于在原图中寻找子图片坐标的类

    在网上找了好久,没有一个现成的例子,自己也发帖子可惜没有找到好办法. 只好自己动手写了, 以下为个人想法所写,算法可能不会太好,如果各位有好的例子发来大家一起分享一下. 这个类主要实现了图片坐标查找功 ...

  10. EasyNVR摄像机无插件直播安装使用错误原因解析

    背景需求 EasyNVR(www.easynvr.com)摄像机无插件直播流媒体服务器对于互联网的视频直播还是有着一定的贡献的.为了方便用户的体验使用,我们也在互联网上放置了对应的试用版本,并且也会随 ...