Understanding RequireJS for Effective JavaScript Module Loading
Modular programming is used to break large applications into smaller blocks of manageable code. Module based coding eases the effort for maintenance and increases reusability. However, managing dependencies between modules is a major concern developers face throughout the application development process. RequireJS is one of the most popular frameworks around for managing dependencies between modules. This tutorial examines the need for modularized code, and shows how RequireJS can help.
Loading JavaScript Files
Large applications often require a number of JavaScript files. Generally, they are loaded one by one using <script>
tags. Additionally, each file can potentially be dependent on other
files. The most common example would be jQuery plugins, which are all
dependent upon the core jQuery library. Therefore, jQuery must be loaded
before any of its plugins. Let’s look at a simple example of JavaScript
file loading in real applications. Assume we have the following three
JavaScript files.
purchase.js
function purchaseProduct(){
console.log("Function : purchaseProduct");
var credits = getCredits();
if(credits > 0){
reserveProduct();
return true;
}
return false;
}
products.js
function reserveProduct(){
console.log("Function : reserveProduct");
return true;
}
credits.js
function getCredits(){
console.log("Function : getCredits");
var credits = "100";
return credits;
}
In this example, we are trying to purchase a product. First, it checks whether enough credits are available to purchase the product. Then, upon credit validation, it reserves the product. Another script, main.js
, initializes the code by calling purchaseProduct()
, as shown below.
var result = purchaseProduct();
What Can Go Wrong?
In this example, purchase.js
depends upon both credits.js
and products.js
. Therefore, those files need to be loaded before calling purchaseProduct()
. So, what would happen if we included our JavaScript files in the following order?
<script src="products.js"></script>
<script src="purchase.js"></script>
<script src="main.js"></script>
<script src="credits.js"></script>
Here, initialization is done before credits.js
is loaded. This will result in the error shown below. And this example only requires three JavaScript files. In a much larger project, things can easily get out of control. That’s where RequireJS comes into the picture.
Understanding RequireJS for Effective JavaScript Module Loading的更多相关文章
- RequireJS使用小结1——for Effective JavaScript Module Loading
1. require和define的区别 The require() function is used to run immediate functionalities, while define() ...
- Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br
/** * Created with JetBrains PhpStorm. * User: scotty * Date: 28/08/2013 * Time: 19:39 */ ;(function ...
- 理解用requireJs 来实现javascript的模块化加载
这是我看到的一片关于requirejs的初学者的文章,写的不错,下面结合自己的理解记录一下: 原文:http://www.sitepoint.com/understanding-requirejs-f ...
- JavaScript Module Pattern: In-Depth
2010-03-12 JavaScript Module Pattern: In-Depth The module pattern is a common JavaScript coding patt ...
- 小王子浅读Effective javascript(一)了解javascript版本
哈哈,各位园友新年快乐!愚安好久没在园子里写东西了,这次决定针对javascript做一个系列,叫做<小王子浅读Effective javascript>,主要是按照David Herma ...
- RequireJS 是一个JavaScript模块加载器
RequireJS 是一个JavaScript模块加载器.它非常适合在浏览器中使用, 它非常适合在浏览器中使用,但它也可以用在其他脚本环境, 就像 Rhino and Node. 使用RequireJ ...
- javascript module system all in one
javascript module system all in one AMD & CMD https://github.com/amdjs/amdjs-api/wiki/AMD http:/ ...
- [Effective JavaScript 笔记] 第4条:原始类型优于封闭对象
js有5种原始值类型:布尔值.数字.字符串.null和undefined. 用typeof检测一下: typeof true; //"boolean" typeof 2; //&q ...
- [Effective JavaScript 笔记] 第5条:避免对混合类型使用==运算符
“1.0e0”=={valueOf:function(){return true;}} 是值是多少? 这两个完全不同的值使用==运算符是相等的.为什么呢?请看<[Effective JavaSc ...
随机推荐
- MySQL索引,备份和还原
1.索引 1.索引是占硬盘空间 ,也是按页存放的 . 思考题:一个索引页,(数据页) 占用多少个字节 .SQL Server 8192个字节 2.索引:是一种有效组合数据的方式,为了快速查找指定 ...
- c++ Oracle OCCI 编程
转载备忘:http://blog.sina.com.cn/s/blog_53a72add01015zj4.html 关于occi编程可以参考的链接: http://blog.itpub.net/162 ...
- 【vuejs面试题】务必熟知的vuejs面试题「务必收藏」
如果能帮到你,点个赞吧,务必熟知的vuejs面试题「务必收藏」 vuejs 基础必备 1.active-class 是哪个组件的属性?嵌套路由怎么定义 (1).active-class 是 vue-r ...
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
出现错误 Client does not support authentication protocol requested by server; consider upgrading MySQL c ...
- python 调用dll 动态链接库 结构体参数及回调函数等示例
结构体示例: 这里是 C 代码的部分,主要是结构体的声明和回调函数定义. // 新版本定义 typedef enum { DevCard, DevLocator, DevReader } DevTyp ...
- tensorflow 中 inter_op 和 intra_op
[root@localhost custom-resnet-v2]# cat runme.sh #python demo_slim.py -h #python demo_slim.py --cpu_n ...
- 强化学习(Reinfment Learning) 简介
本文内容来自以下两个链接: https://morvanzhou.github.io/tutorials/machine-learning/reinforcement-learning/ https: ...
- 【长期计划】Atcoder题目泛做
之前学长跟我说的是700-的应该都能自己做? 然后1000-的应该都能有一定的思路? 记不清了 但总之是要智力康复一下 又加上文化课比较紧 所以这个大概就会是长期计划了 ————————————分鸽线 ...
- wepy-wxss报错
慢慢积攒下wepy 的一些BUG吧 1.页面在page目录下明明删除了某个子页面文件,打开wepy却一直报错!wxml报错或者wxss报错,提示的页面我为了排错都直接delete掉了,还是报错???思 ...
- 基于http方式搭建YUM源服务器
基于http方式搭建YUM源服务器 (2012-09-21 11:59:14) 转载▼ 标签: yum linux lnmp lamp http 分类: Linux 为了方便公司80多台Linux服务 ...