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的更多相关文章

  1. RequireJS使用小结1——for Effective JavaScript Module Loading

    1. require和define的区别 The require() function is used to run immediate functionalities, while define() ...

  2. 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 ...

  3. 理解用requireJs 来实现javascript的模块化加载

    这是我看到的一片关于requirejs的初学者的文章,写的不错,下面结合自己的理解记录一下: 原文:http://www.sitepoint.com/understanding-requirejs-f ...

  4. JavaScript Module Pattern: In-Depth

    2010-03-12 JavaScript Module Pattern: In-Depth The module pattern is a common JavaScript coding patt ...

  5. 小王子浅读Effective javascript(一)了解javascript版本

    哈哈,各位园友新年快乐!愚安好久没在园子里写东西了,这次决定针对javascript做一个系列,叫做<小王子浅读Effective javascript>,主要是按照David Herma ...

  6. RequireJS 是一个JavaScript模块加载器

    RequireJS 是一个JavaScript模块加载器.它非常适合在浏览器中使用, 它非常适合在浏览器中使用,但它也可以用在其他脚本环境, 就像 Rhino and Node. 使用RequireJ ...

  7. javascript module system all in one

    javascript module system all in one AMD & CMD https://github.com/amdjs/amdjs-api/wiki/AMD http:/ ...

  8. [Effective JavaScript 笔记] 第4条:原始类型优于封闭对象

    js有5种原始值类型:布尔值.数字.字符串.null和undefined. 用typeof检测一下: typeof true; //"boolean" typeof 2; //&q ...

  9. [Effective JavaScript 笔记] 第5条:避免对混合类型使用==运算符

    “1.0e0”=={valueOf:function(){return true;}} 是值是多少? 这两个完全不同的值使用==运算符是相等的.为什么呢?请看<[Effective JavaSc ...

随机推荐

  1. iScroll使用参考

    分享是传播.学习知识最好的方法 以下这篇文章是iScroll.js官网的中文翻译,尽管自己英文不好,但觉得原作者们翻译的这个资料还是可以的,基本用法介绍清楚了.如果你英文比较好的话,可以看看官网的资料 ...

  2. phpStudy配置sql、oracle---博主摘录

    引用 :https://www.cnblogs.com/myBlogInWork/p/8657125.html 由于工作需要,要用到php+oracle写个项目,故而有了以下内容: 本来以为php有默 ...

  3. [BZOJ3133] [Baltic2013]ballmachine(树上倍增+堆)

    [BZOJ3133] [Baltic2013]ballmachine(树上倍增+堆) 题面 有一个装球机器,构造可以看作是一棵树.有下面两种操作: 从根放入一个球,只要下方有空位,球会沿着树滚下.如果 ...

  4. CSS 毛玻璃效果

    效果图: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <met ...

  5. Vue切换页面时中断axios请求

    一.概述 在Vue单页面开发过程中,遇到这样的情况,当我切换页面时,由于上一页面请求执行时间长,切换到该页面时,还未执行完,这时那个请求仍会继续执行直到请求结束,此时将会影响页面性能,并且可能对现在页 ...

  6. mysql中的substring()截取字符函数

    substring(参数1,参数2,参数3),其中三个参数分别表示:参数1表示需要截取的字符串,参数2表示从字符串的那个位置开始截取(字符串下标从1开始),参数3表示要截取多少位,如果不写,表示截取从 ...

  7. 将Medium中的博客导出成markdown

    Medium(https://medium.com)(需要翻墙访问)是国外非常知名的一个博客平台.上面经常有很多知名的技术大牛在上面发布博客,现在一般国内的搬运的技术文章大多数都是来自于这个平台. M ...

  8. mysql常见函数及其用例

    函数调用:select 函数名(实参列表) [from 表]; 函数分类: 1.单行函数 如 concat.length.ifnull等. 2.分组函数 功能:做统计使用,又称为统计函数.聚合函数.组 ...

  9. Redis 内存满了怎么办? Redis的内存淘汰策略

    https://juejin.im/post/5d674ac2e51d4557ca7fdd70 Redis占用内存大小 我们知道Redis是基于内存的key-value数据库,因为系统的内存大小有限, ...

  10. Nginx服务项的基本配置

    由于Nginx配置项较多,把他们按照用户使用时的预期功能分为以下4类: 1.调试,定位问题的配置项 2.正常运行必备配置项 3.优化性能配置项 4.事件类配置项 1. 用于调试进程,定位问题的配置项 ...