zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    python自动化测试-D9-学习笔记之二(线程锁)
    python习题:封装好的测试报告(report.py)
    python自动化测试-D9-学习笔记之二(守护线程)
    python自动化测试-D9-学习笔记之二(多线程)
    python自动化测试-D9-学习笔记之一(线程池)
    python自动化测试-D9-学习笔记之一(unittest模块)
    python习题:写一个备份数据库的脚本
    python习题:【多线程】有100个数据,启动5个线程,每个线程分20个数据,怎么把这20个数据分别传给每个线程。
    自然语言处理NLTK之入门
    python画一颗拳头大的💗
  • 原文地址:https://www.cnblogs.com/chucklu/p/11094616.html
Copyright © 2011-2022 走看看