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的顺 ...
随机推荐
- Java中的克隆(CLONE)
解读克隆 编程过程中我们常常遇到如下情况: 假设有一个对象object,在某处又需要一个跟object一样的实例object2,强调的是object和object2是两个独立的实例,只是在 开始的时候 ...
- 广州Uber优步司机奖励政策(12月28日到1月3日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Oracle Spool详解
转自:http://blog.sina.com.cn/s/blog_6bccf0360101hzsh.html 1.spool的作用是什么? spool的作用可以用一句话来描述:在sqlplus中用来 ...
- SEO学习之路
一.入门建站篇 1.Wamp集成环境安装 2.Wamp集成环境配置多站点 3.DedeCMS安装及目录结构 4.DedeCMS源码安装 5.DedeCMS官方手册 未完待续...
- JS中undefined与null的区别
1.概述: 在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? ...
- 【实验 1-2】编写一个简单的 UDP 服务器和 UDPP 客户端程序。程序均为控制台程序窗口。
1.服务器 #include<winsock2.h> //包含头文件#include<stdio.h>#include<windows.h>#pragma comm ...
- JavaScript兼容问题汇总[实时更新]
印象笔记链接地址:点我查看 遇到问题不断更新中-- [转载请注明出处-HTML5自由者]
- UI Prototype Design IDE( 界面原型设计工具 )
UI Prototype Design IDE( 界面原型设计工具 ) 如何用工具去与客户进行交流,互动,定义要做的系统,什么什么的... 0.Balsamiq Mockups http://ww ...
- Linux设备文件自动生成
第一种是使用mknod手工创建:# mknod <devfilename> <devtype> <major> <minor> 第二种是自动创建设备节点 ...
- 内核与ramdisk到底是什么关系
转自:http://www.lupaworld.com/forum.php?mod=viewthread&tid=61425 原名:内核与ramdisk到底是什么关系? 个人Notes: ...