1. require和define的区别

The require() function is used to run immediate functionalities, while define() is used to define modules for use in multiple locations.

  • require()——用于一次性定义的语句或模块,或立即执行的语句或模块
  • define()—— 用于可以重用的模块,可以放在不同的地方

2. 管理依赖文件的载入顺序——Managing the Order of Dependent Files

RequireJS uses Asynchronous Module Loading (AMD) for loading files. Each dependent module will start loading through asynchronous requests in the given order. Even though the file order is considered, we cannot guarantee that the first file is loaded before the second file due to the asynchronous nature. So, RequireJS allows us to use the shim config to define the sequence of files which need to be loaded in correct order. Let’s see how we can create configuration options in RequireJS.

RequireJS使用异步模块载入机制(AMD),即每个独立模块都是通过异步请求载入,就是说无法保证第一个文件在第二个文件之前先行载入,为此,RequireJS使用shim来强制定义文件载入的顺序,如以下代码:

requirejs.config({
shim: {
'source1': ['dependency1','dependency2'],
'source2': ['source1']
}
});

Under normal circumstances these four files will start loading in the given order. Here, source2 depends on source1. So, once source1 has finished loading, source2 will think that all the dependencies are loaded. However, dependency1 and dependency2 may still be loading. Using the shim config, it is mandatory to load the dependencies before source1. Hence, errors will not be generated.

上述定义表示: source2依赖于source1,只有source1完成载入后才载入source2;source1依赖于dependency1和dependency2,只有dependency1和dependency2完成载入后才载入source1,故整个载入顺序为:

dependency1,dependency2(没有规定该两个文件的载入顺序)--> source1 --> source2

define(["dependency1","dependency2","source1","source2"], function() {

);

RequireJS使用小结1——for Effective JavaScript Module Loading的更多相关文章

  1. Understanding RequireJS for Effective JavaScript Module Loading

    Modular programming is used to break large applications into smaller blocks of manageable code. Modu ...

  2. 小王子浅读Effective javascript(一)了解javascript版本

    哈哈,各位园友新年快乐!愚安好久没在园子里写东西了,这次决定针对javascript做一个系列,叫做<小王子浅读Effective javascript>,主要是按照David Herma ...

  3. javascript module system all in one

    javascript module system all in one AMD & CMD https://github.com/amdjs/amdjs-api/wiki/AMD http:/ ...

  4. JavaScript Module Pattern: In-Depth

    2010-03-12 JavaScript Module Pattern: In-Depth The module pattern is a common JavaScript coding patt ...

  5. [Effective JavaScript 笔记] 第4条:原始类型优于封闭对象

    js有5种原始值类型:布尔值.数字.字符串.null和undefined. 用typeof检测一下: typeof true; //"boolean" typeof 2; //&q ...

  6. [Effective JavaScript 笔记] 第5条:避免对混合类型使用==运算符

    “1.0e0”=={valueOf:function(){return true;}} 是值是多少? 这两个完全不同的值使用==运算符是相等的.为什么呢?请看<[Effective JavaSc ...

  7. [Effective JavaScript 笔记]第3章:使用函数--个人总结

    前言 这一章把平时会用到,但不会深究的知识点,分开细化地讲解了.里面很多内容在高3等基础内容里,也有很多讲到.但由于本身书籍的篇幅较大,很容易忽视对应的小知识点.这章里的许多小提示都很有帮助,特别是在 ...

  8. [Effective JavaScript 笔记]第27条:使用闭包而不是字符串来封装代码

    函数是一种将代码作为数据结构存储的便利方式,代码之后可以被执行.这使得富有表现力的高阶函数抽象如map和forEach成为可能.它也是js异步I/O方法的核心.与此同时,也可以将代码表示为字符串的形式 ...

  9. [Effective JavaScript 笔记]第28条:不要信赖函数对象的toString方法

    js函数有一个非凡的特性,即将其源代码重现为字符串的能力. (function(x){ return x+1 }).toString();//"function (x){ return x+ ...

随机推荐

  1. python标准库介绍——2 os.path模块详解

    == os.path 模块 == ``os.path`` 模块包含了各种处理长文件名(路径名)的函数. 先导入 (import) ``os`` 模块, 然后就可以以 ``os.path`` 访问该模块 ...

  2. Java中的static关键字的用法

    1.静态方法 static:通常在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法 声明为static的方法有以下几条限制: (1)它们仅能调用其他的static方法. (2 ...

  3. UVA 1264 - Binary Search Tree(BST+计数)

    UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...

  4. 递归查询SQL语句

    USE [DB] GO /****** Object: View [dbo].[v_menus] Script Date: 02/24/2014 15:55:45 ******/ SET ANSI_N ...

  5. Eclipse中设置文件编码

    如果你在使用某个editor进行开发的话,文件编码就由改editor解决即可 Eclipse中也有这个功能,帮你设置文件的编码,选择Edit->Set Encoding即可 注意,这个选项针对不 ...

  6. mysql 存入乱码问题

    在使用mysql开发是,遇到一问题,java脚本里面的中文很正常,持久化之后数据库里面的数据则变成乱码,解决方式,在spring配置文件连接中加入指定编码格式,有些系统不需要,有些服务器系统需要,统一 ...

  7. linux 基本配置tab键和显示行号 和中文输入法

    一.仅设置当前用户的Tab键宽度 输入命令:vim ~/.vimrc 然后:set tabstop=4   //我这里将Tab键的宽度设置为4 保存:ctrl+z+z(或:wq!) OK! 二.设置所 ...

  8. c++派生类的访问控制权限

    派生类必须通过使用类派生列表, 明确指出它是从哪个(哪些)基类继承而来的.类派生列表的形式是:首先是一个冒号,后面紧跟以逗号分隔的基类列表(可以多继承,但一般不使用多继承),其中每个基类前面可以有以下 ...

  9. multi-mechanize error: can not find test script: v_user.py问题

    从github上下载,安装multi-mechanize,新建工程,运行工程报错. 环境: win7-x64, python 2.7 multi-mechanize can not find test ...

  10. JDK和OpenJDK的区别

    简言之,open jdk 是 oracle(sun) jdk的精简版,如果本地运行有问题,linux服务器有问题,首先排除open jdk版本的问题. 使用过LINUX的人都应该知道,在大多数LINU ...