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.

原文地址:https://www.cnblogs.com/chucklu/p/11094616.html

时间: 2024-10-13 02:01:29

Understanding RequireJS for Effective JavaScript Module Loading的相关文章

RequireJS is a JavaScript file and module loader

RequireJS RequireJS 是一个JavaScript模块加载器.它非常适合在浏览器中使用, 它非常适合在浏览器中使用,但它也可以用在其他脚本环境, 就像 Rhino and Node. 使用RequireJS加载模块化脚本将提高代码的加载速度和质量. /* --- RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in ot

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(global){ "use strict"; var M = function() { // Constructor. arguments are passed // from Module() call. this refers to m. function init() { meth

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

这是我看到的一片关于requirejs的初学者的文章,写的不错,下面结合自己的理解记录一下: 原文:http://www.sitepoint.com/understanding-requirejs-for-effective-javascript-module-loading/ Modular programming is used to break large applications into smaller blocks of manageable code. Module based c

javascript闭包(Effective JavaScript读书笔记)

Effective JavaScript:编写高质量JavaScript代码的68个有效方法: Item 11:   Get Comfortable with Closures Closures may be an unfamiliar concept to programmers coming from languages that don’t support them. And they may seem intimidating at first. But rest assured tha

Effective JavaScript Item 24 使用一个变量来保存arguments的引用

本系列作为Effective JavaScript的读书笔记. 假设需要一个API用来遍历若干元素,像下面这样: var it = values(1, 4, 1, 4, 2, 1, 3, 5, 6); it.next(); // 1 it.next(); // 4 it.next(); // 1 相应的实现可以是: function values() { var i = 0, n = arguments.length; return { hasNext: function() { return

转载翻译文章:JavaScript Module Pattern: In-Dept

# JavaScript Module Pattern: In-Depth # 转载翻译文章:JavaScript Module Pattern: In-Depth*原文*:http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html 模块模式是一种常见的js开发编码模式.通常来说,它很好理解,但还有一些高级应用并没有得到太多关注.这篇文章,我将回顾基础知识,并介绍一些真正了不起的高级主题,其中包括一个我认为极为原始的

Effective JavaScript Item 25 使用bind方法来得到一个固定了this指向的方法

本系列作为Effective JavaScript的读书笔记. 当需要将方法抽取出来作为回调函数使用的时候,常常会因为this的指向不明而发生错误,比如: var buffer = { entries: [], add: function(s) { this.entries.push(s); }, concat: function() { return this.entries.join(""); } }; 如果想利用其中的add作为回调函数对一组数据进行添加: var source

Effective JavaScript Item 23 永远不要修改arguments对象

本系列作为Effective JavaScript的读书笔记. arguments对象只是一个类似数组的对象,但是它并没有数组对象提供的方法,比如shift,push等.因此调用诸如:arguments.shift(),arguments.push()是错误的. 在Item 20和Item 21中,知道了函数对象上存在call和apply方法,那么是不是可以利用它们来让arguments也能够利用数组的方法呢: function callMethod(obj, method) { var shi

【JavaScript】Understanding callback functions in Javascript

Callback functions are extremely important in Javascript. They’re pretty much everywhere. Originally coming from a more traditional C/Java background I had trouble with this (and the whole idea of asynchronous programming), but I’m starting to get th