原文地址: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. shell中的替换

    shell中如果存在一些特殊的字符,就需要进行替换,可进行命令替换.变量替换.转义替换 1.转义字符的替换 shell中包含以下的转移字符 \a   响铃警报\\   反斜杠 \b  退格(删除键) ...

  2. Javascript:浮动的导航条

    代码主体及说明 Javascript部分: /** * @函数名:flexScroll * @功能:滚动超出一定高度,指定元素悬浮 * @两个参数:target_id:目标元素id;topEle:限定 ...

  3. [转]ANDROID仿IOS微信滑动删除_SWIPELISTVIEW左滑删除例子

    转载:http://dwtedx.sinaapp.com/itshare_290.html 本例子实现了滑动删除ListView的Itemdemo的效果.大家都知道.这种创意是来源于IOS的.左滑删除 ...

  4. Ignatius and the Princess III --undo

    Ignatius and the Princess III Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (J ...

  5. saltstack对递归依赖条件(死循环依赖)的处理

    本文将对saltstack中状态文件中require条件产生死循环的情形进行简单的测试和分析 测试思路: 写一个包含递归依赖条件的状态文件,进行测试:      A依赖于B      B依赖于C    ...

  6. 带搜索功能,支持绑定对象到节点的TreeView辅助类

    特点: 1.支持数叶子节点与对象绑定 2.支持xml导入,且数据类相关的xml可自定义,只和泛型的实现有关 3.支持节点搜索功能,可在树结构上要求只显示部分节点 4.用C#编写,但与平台关联性低,可移 ...

  7. java下properties属性文件操作

    package cn.stat.p1.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...

  8. JSON.parse(),JSON.stringify(),jQuery.parseJSON()的用法

    1. JSON.parse(jsonString): 在一个字符串中解析出JSON对象 var str = '[{"href":"baidu.com",&quo ...

  9. HTML写的第一个邮箱登陆界面

    自己动手去写才会有收获,宁可模仿也不要全部复制粘贴 不说了,直接上代码.CSS有注释,适合新手. <!doctype html> <html> <head> < ...

  10. Playground

    题意 :求被两点分割的凸包面积的较小值    题意已经给出顺时针啦 就是求以某一个点 和其他所有相邻点组成三角形的面积,然后sum存和求两点的时候就求出那两点的之间所有三角形的和再减掉0点和那两点的面 ...