原文地址: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. grouth:全栈工程师指南

    语言学习 curl -v baidu.com 域名请求DNS服务器得到网站服务器的IP,http是80端口,https是443端口.查到ip拿到HTML,JS,CSS后就开始渲染这个页面了. 1.Pa ...

  2. 汉得第二次考核答案整理(通信,IO,文件等)

    1, (8 分 ) 使用程序从网上下载 pdf, 网址为http://files.saas.hand-china.com/java/target.pdf,保存在本地,编程时使用带缓冲的读写,将需要保证 ...

  3. (转) xcodebuild和xcrun自动化编译ipa包 笔记

    转自:http://blog.csdn.net/totogo2010/article/details/8883100 打包过程 xcodebuild负责将工程源文件编译成xxx.app xcrun负责 ...

  4. 程序猿的道路~~(How to be a programmer?)

    程序猿的道路其实很简单,主要就是三条: Learn (学习), Practice(练习), Summary(总结) 推荐给新手程序猿两篇文章: 给程序员新手的一些建议 程序员技术练级攻略 当然了,整个 ...

  5. 300元差价选谁好 魅蓝note对比魅蓝手机

    http://mobile.pconline.com.cn/608/6089437.html [PConline 对比评测]999元的魅蓝note和699元的魅蓝手机先后被发布,代表着魅族中低端手机已 ...

  6. 关于退运美国转基因玉米含有MRI 162转基因成分的质疑

    6月30日,新华社刊出文章"我国退运125.2万吨进口美国转基因玉米",读后有感. 文章说:国家质检总局办公厅副主任陆春明30日介绍,截至今年6月16日,全国出入境检验检疫机构共在 ...

  7. 中国大推力矢量发动机WS15 跨入 世界先进水平!

    "太行"WS-15让俄闭嘴令美叹服 歼20试飞向世界证明,中国军工世界一流,并有望与美英法争夺新一代航空发动机桂冠.笔者请教解放军专家证实:中国四代机所配套的两台18吨推力的WS- ...

  8. FoxOne---一个快速高效的BS框架

    FoxOne---一个快速高效的BS框架--(1) FoxOne---一个快速高效的BS框架--(2) FoxOne---一个快速高效的BS框架--(3) FoxOne---一个快速高效的BS框架-- ...

  9. Python对文件的操作(转)

    一.文件对象 我理解的文件对象就是一个接口,通过这个接口对文件进行相关操作. <Python 核心编程>上说的很晦涩,这里没有深刻理解到,希望有人能解释给我听. >>> ...

  10. oracle数据库exp/imp命令详解

    转自http://wenku.baidu.com/link?url=uD_egkkh7JtUYJaRV8YM6K8CLBT6gPJS4UlSy5WKhz46D9bnychTPdgJGd7y6UxYtB ...