requireJS define require
原文地址:http://blog.csdn.net/xiaogou56a/article/details/21340213
define 用来定义模块
require 用来加载模块
1
因为定义一个模块,可能会依赖其他模块,当然最简单的情况下是不依赖其他模块,这时就可以这样写:
- //Inside file my/shirt.js:
- define({
- color: "black",
- size: "unisize"
- });
官方解释: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工作,假设它也不依赖其他模块,这时可以这样写:
- //my/shirt.js now does setup work
- //before returning its module definition.
- define(function () {
- //Do setup work here
- return {
- color: "black",
- size: "unisize"
- }
- });
官方解释: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工作,那么这个时候可以这样写:
- //my/shirt.js now has some dependencies, a cart and inventory
- //module in the same directory as shirt.js
- define(["./cart", "./inventory"], function(cart, inventory) {
- //return an object to define the "my/shirt" module.
- return {
- color: "blue",
- size: "large",
- addToCart: function() {
- inventory.decrement(this);
- cart.add(this);
- }
- }
- }
- );
被依赖的模块会作为参数一次传入到那个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
- //Explicitly defines the "foo/title" module:
- define("foo/title",
- ["my/cart", "my/inventory"],
- function(cart, inventory) {
- //Define foo/title object in here.
- }
- );
相信你一看便理解了,不过它里面的学问可以在没事的时候去看看官方的文档说明,也许很有意思的哦。
还有好多其他情况,但记住本质,define是用来定义模块的,require是用来加载模块的,整个库的开发考虑的情况比较多,比如:为了兼容以前的代码,为了适应某些库的使用,某些转换工具的使用,元编程的应用,等等。只要我们抓住本质,理解意思,具体的格式参考官网,只要别人用了,就肯定是合法的,就肯定是有根源的,今天到此为止。
requireJS define require的更多相关文章
- requirejs——define——普通模块
一.普通模块可能包含的内容: 一个模块对应着一个js文件,由于模块可能包含着以下三种内容:模块名.依赖模块.返回给其他模块使用的参数:因此js文件根据包含内容的不同而写法不同. 一.传统的js脚本文件 ...
- requirejs define a module
https://requirejs.org/docs/api.html#define Define a Module § 1.3 A module is different from a tradit ...
- 使用maven结合requirejs管理前端脚本
已有的web项目,一直使用Maven做工程管理,现阶段前端调整为使用requirejs来负责模块加载依赖,同时使用jasmine来完成前端的UT. 便与在maven下统一管理,简单整理了下合在一起的使 ...
- Backbone Collection
http://yujianshenbing.iteye.com/blog/1748826 如果将一个Model对象比喻成数据库中的一条记录,那么Collection就是一张数据表.它表示为一个模型集合 ...
- backbone Model
requirejs.config({ baseUrl: 'js/lib', paths:{ app: '../app' } }) // Start the main app logic. //requ ...
- requireJs require.config公共配置
//场景:让require.config配置文件成一个公共文件,每个页面引用这个公共配置 //方式一样例: require.config({ baseUrl: (function () { var p ...
- -_-#【RequireJS】Define a Module
define({ color: 'black', size: 'unisize' }) define(function() { // Do setup work here return { color ...
- 实现一个类 RequireJS 的模块加载器 (二)
2017 新年好 ! 新年第一天对我来说真是悲伤 ,早上兴冲冲地爬起来背着书包跑去实验室,结果今天大家都休息 .回宿舍的时候发现书包湿了,原来盒子装的牛奶盖子松了,泼了一书包,电脑风扇口和USB口都进 ...
- 使用RequireJS并实现一个自己的模块加载器 (一)
RequireJS & SeaJS 在 模块化开发 开发以前,都是直接在页面上引入 script 标签来引用脚本的,当项目变得比较复杂,就会带来很多问题. JS项目中的依赖只有通过引入JS的顺 ...
随机推荐
- jqTransform——学习(1)
官网:http://www.dfc-e.com/metiers/multimedia/opensource/jqtransform/ 转载:http://www.helloweba.com/view- ...
- (转)利用ant在Mac 下自动化打包签名Android程序
1.创建一个android项目 参数说明:-k /--package 为命名空间(包名)-a /--name 工程名-p /--path 项目 -t 为目标平台的版本编号 命令:./android c ...
- Collision使用 获取其组件执行变色操作
using UnityEngine; using System.Collections; public class CyCollision : MonoBehaviour { void OnColli ...
- dialog中的button动态设置为disable[转]
我们再写dialog的时候,会时常有这样一种需求,希望通过某些条件将dialog的button设置为disable的. 基本的命令就是将“确定”这个button设置为disable(false). 如 ...
- android之字体阴影效果
今天刚刚好做了个字体阴影的效果,感觉加上了阴影的效果立体感十足啊!写了个简单的demo与大家分享下!主要是以下四个属性 android:shadowColor 阴影的颜色 android:shad ...
- AxisFault另外一个问题
出现以下情况,能够是proxy.setEndpoint(endpoint);中endpoint不正确导致 因该是:endpoint = http://127.0.0.1/8080/项目名/servic ...
- macos10.8.5原版系统dmg转iso
网上非常多资料说使用UltraISO打开macos10.8.5, 将InstallESD.dmg提取出来, 然后再用UltraISO打开InstallESD.dmg,点转换格式, 选择iso, 然后用 ...
- mybati的存储过程
这里我就以的存储过程为例,大家一起学习一下,
- javaScrip中的“?”
例如window.location.href="./user/userUpdate?id="+id; 在这里“?”是传入参数或是带个参数id,这样就可以获得到主键了. String ...
- java下管道流 PipedOutputStream 与PipedInputStream
package cn.stat.p2.demo; import java.io.IOException; import java.io.PipedInputStream; import java.io ...