原文地址: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. DB2高级安装

     学习从安装开始,哈哈.这里记下  Windows 和 UNIX/Linux安装DB2的各种方式,不同,及具体的一些细节.    Windows下向导化安装:            setup.exe ...

  2. [转载]ios app 发布遇到的问题uinewsstandapp=true

    原文地址:ios app 发布遇到的问题uinewsstandapp=true 作者:Capacity To include newsstand features, the info.plist mu ...

  3. xml的加密和解密

    xml加密(XML Encryption)是w3c加密xml的标准.这个加密过程包括加密xml文档的元素及其子元素,通过加密,xml的初始内容将被替换,但其xml格式仍然被完好的保留. 介绍我们有3个 ...

  4. next数组

    首先看看next数组值的求解方法例如: 模式串 a b a a b c a c next值 0 1 1 2 2 3 1 2               next数组的求解方法是:第一位的next值为0 ...

  5. 小学生之KTV播放原理

    第一步: 创建一个Song类 //歌曲名称 public  string SongName { get; set; } //歌曲路劲 public string SongPath { get; set ...

  6. MVC4 数据库连接字串

    1.SQL Server <add name="DBEntities" connectionString="Data Source=.;Initial Catalo ...

  7. 学习Android NestedScroll

    NestedScrollingChildHelper 这是一个用于实现子视图嵌套滚动的辅助类,并提供对Android 5.0之前版本的前兼容. View要作为嵌套滚动中的Child,要在构造方法中实例 ...

  8. Path对象

    Path是连续的Segment的集合,除了 Path 的第一个Segment和最后一个Segment外,其余的Segment的起始点都是前一个Segment的终止点,即Path对象的中的Segment ...

  9. [c#]asp.net开发微信公众平台(2)多层架构框架搭建和入口实现

    上篇已经设计出比较完善的数据库了,这篇开始进入代码.  首先把上篇设计的数据库脚本在数据库中执行下,生成数据库,然后在VS中建立项目,为了方便理解和查看,我设计的都是很直白的类名和文件名,没有命名空间 ...

  10. 如何快速恢复MyEclipse的默认主题

    这里天在研究主题,到网上找了一些主题导入,可是有一部分主题导入后不能通过preference选项进行恢复默认主题!那怎么办?有没有别的办法! 在网上找了一些答案,有更改工作空间的办法,也有替换.set ...