原文地址:http://blog.csdn.net/xiaogou56a/article/details/21340213

define 用来定义模块

require 用来加载模块

1

因为定义一个模块,可能会依赖其他模块,当然最简单的情况下是不依赖其他模块,这时就可以这样写:

  1. //Inside file my/shirt.js:
  2. define({
  3. color: "black",
  4. size: "unisize"
  5. });

官方解释:If the module does not have any dependencies, and it is just a collection of name/value pairs, then just pass an object literal to define():

2

定义一个模块,也可能需要先做一些setup工作,假设它也不依赖其他模块,这时可以这样写:

  1. //my/shirt.js now does setup work
  2. //before returning its module definition.
  3. define(function () {
  4. //Do setup work here
  5. return {
  6. color: "black",
  7. size: "unisize"
  8. }
  9. });

官方解释:If the module does not have dependencies, but needs to use a function to do some setup work, then define itself, pass a function to define():

3

定义一个模块,可能会很复杂,既依赖其它一些个模块,又需要一些setup工作,那么这个时候可以这样写:

  1. //my/shirt.js now has some dependencies, a cart and inventory
  2. //module in the same directory as shirt.js
  3. define(["./cart", "./inventory"], function(cart, inventory) {
  4. //return an object to define the "my/shirt" module.
  5. return {
  6. color: "blue",
  7. size: "large",
  8. addToCart: function() {
  9. inventory.decrement(this);
  10. cart.add(this);
  11. }
  12. }
  13. }
  14. );

被依赖的模块会作为参数一次传入到那个function中。

看官方解释:If the module has dependencies, the first argument should be an array of dependency names, and the second argument should be a definition function. The function will be called to define the module once all dependencies have loaded. The function should return an object that defines the module. The dependencies will be passed to the definition function as function arguments, listed in the same order as the order in the dependency array:

4

Define a Module with a Name

  1. //Explicitly defines the "foo/title" module:
  2. define("foo/title",
  3. ["my/cart", "my/inventory"],
  4. function(cart, inventory) {
  5. //Define foo/title object in here.
  6. }
  7. );

相信你一看便理解了,不过它里面的学问可以在没事的时候去看看官方的文档说明,也许很有意思的哦。

还有好多其他情况,但记住本质,define是用来定义模块的,require是用来加载模块的,整个库的开发考虑的情况比较多,比如:为了兼容以前的代码,为了适应某些库的使用,某些转换工具的使用,元编程的应用,等等。只要我们抓住本质,理解意思,具体的格式参考官网,只要别人用了,就肯定是合法的,就肯定是有根源的,今天到此为止。

requireJS define require的更多相关文章

  1. requirejs——define——普通模块

    一.普通模块可能包含的内容: 一个模块对应着一个js文件,由于模块可能包含着以下三种内容:模块名.依赖模块.返回给其他模块使用的参数:因此js文件根据包含内容的不同而写法不同. 一.传统的js脚本文件 ...

  2. requirejs define a module

    https://requirejs.org/docs/api.html#define Define a Module § 1.3 A module is different from a tradit ...

  3. 使用maven结合requirejs管理前端脚本

    已有的web项目,一直使用Maven做工程管理,现阶段前端调整为使用requirejs来负责模块加载依赖,同时使用jasmine来完成前端的UT. 便与在maven下统一管理,简单整理了下合在一起的使 ...

  4. Backbone Collection

    http://yujianshenbing.iteye.com/blog/1748826 如果将一个Model对象比喻成数据库中的一条记录,那么Collection就是一张数据表.它表示为一个模型集合 ...

  5. backbone Model

    requirejs.config({ baseUrl: 'js/lib', paths:{ app: '../app' } }) // Start the main app logic. //requ ...

  6. requireJs require.config公共配置

    //场景:让require.config配置文件成一个公共文件,每个页面引用这个公共配置 //方式一样例: require.config({ baseUrl: (function () { var p ...

  7. -_-#【RequireJS】Define a Module

    define({ color: 'black', size: 'unisize' }) define(function() { // Do setup work here return { color ...

  8. 实现一个类 RequireJS 的模块加载器 (二)

    2017 新年好 ! 新年第一天对我来说真是悲伤 ,早上兴冲冲地爬起来背着书包跑去实验室,结果今天大家都休息 .回宿舍的时候发现书包湿了,原来盒子装的牛奶盖子松了,泼了一书包,电脑风扇口和USB口都进 ...

  9. 使用RequireJS并实现一个自己的模块加载器 (一)

    RequireJS & SeaJS 在 模块化开发 开发以前,都是直接在页面上引入 script 标签来引用脚本的,当项目变得比较复杂,就会带来很多问题. JS项目中的依赖只有通过引入JS的顺 ...

随机推荐

  1. 关于C/C++函数指针声明的理解

    [前言] 由于最近对函数指针的理解比较模糊,所有又重新学习了一把关于函数指针的知识,参考了很多书籍和网上的文章.现在本人进行一下分享和总结.本文的其实只是整理和总结别人现有的文章,作为备用参考文档. ...

  2. Java学习之List接口

    List接口 List接口的定义如下: public interface List<E>extends Collection<E> 可以发现List接口时Collection接 ...

  3. C++ primer 中文第三版 阅读笔记 第八章

    一.寄存器对象: 函数中频繁被使用的变量可以加上register就可声明为寄存器对象.对于寄存器对象,假如能够放到寄存器中就会放到寄存器中,放不到的话就放到内存中.比如 register int  a ...

  4. iOS 蓝牙4.0开发

    背景: 1.iOS的蓝牙不能用来传输文件.2.iOS与iOS设备之间进行数据通信,使用gameKit.framework3.iOS与其他非iOS设备进行数据通信,使用coreBluetooth.fra ...

  5. Qt 之容器内的控件全屏

    m_label = new QLabel(); ui->stackedWidget->addWidget(m_label); ui->stackedWidget->setCur ...

  6. jquery之onchange事件2

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  7. c# 交换两个变量

    使用临时变量: 有人会问只使用两个变量交换,怎么办? 不实用临时变量: 第一种: a=a+b; b=a-b; a=a-b; 第二种: 异或:相同是0,不同是1 上面是整型的,那么字符串可以直接异或吗? ...

  8. SQL从入门到基础 - 06 限制结果集范围

    一.限制结果集行数 1. Select top 5* from T_Employee order by FSalary DESC 2. (*)检索按照工资从高到低排序检索从第六名开始一共四个人的信息: ...

  9. sqlite--代码操作

    1.创建数据库 NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainM ...

  10. OSG调试信息显示

    调试信息显示 OSG 可以将各式各样的调试信息输出到std:cout.这在开发OSG 程序时十分有用,你可以借此观察OSG 的执行的各种操作.环境变量OSG_NOTIFY_LEVEL用于控制OSG调试 ...