zoukankan      html  css  js  c++  java
  • RequireJS使用小结1——for Effective JavaScript Module Loading

    1. require和define的区别

    The require() function is used to run immediate functionalities, while define() is used to define modules for use in multiple locations. 

    • require()——用于一次性定义的语句或模块,或立即执行的语句或模块
    • define()—— 用于可以重用的模块,可以放在不同的地方

    2. 管理依赖文件的载入顺序——Managing the Order of Dependent Files

    RequireJS uses Asynchronous Module Loading (AMD) for loading files. Each dependent module will start loading through asynchronous requests in the given order. Even though the file order is considered, we cannot guarantee that the first file is loaded before the second file due to the asynchronous nature. So, RequireJS allows us to use the shim config to define the sequence of files which need to be loaded in correct order. Let’s see how we can create configuration options in RequireJS.

    RequireJS使用异步模块载入机制(AMD),即每个独立模块都是通过异步请求载入,就是说无法保证第一个文件在第二个文件之前先行载入,为此,RequireJS使用shim来强制定义文件载入的顺序,如以下代码:

    requirejs.config({
      shim: {
        'source1': ['dependency1','dependency2'],
        'source2': ['source1']
      }
    });

    Under normal circumstances these four files will start loading in the given order. Here, source2 depends on source1. So, once source1 has finished loading, source2 will think that all the dependencies are loaded. However, dependency1 and dependency2 may still be loading. Using the shim config, it is mandatory to load the dependencies before source1. Hence, errors will not be generated.

    上述定义表示: source2依赖于source1,只有source1完成载入后才载入source2;source1依赖于dependency1和dependency2,只有dependency1和dependency2完成载入后才载入source1,故整个载入顺序为:

                                                    dependency1,dependency2(没有规定该两个文件的载入顺序)--> source1 --> source2

    define(["dependency1","dependency2","source1","source2"], function() {
     
    );
  • 相关阅读:
    Web API 跨域问题
    找不到System.Web.Optimization命名空间
    IIS7配置rewriter
    Windows server 2008 R2实现多用户远程连接 (转)
    DatabaseGenerated(转)
    SQL、LINQ、Lambda 三种用法(转)
    Mvcpager以下各节已定义,但尚未为布局页“~/Views/Shared/_Layout.cshtml”呈现:“Scripts”。
    15-07-16 数据库--增删改查
    看名字测缘分
    String类
  • 原文地址:https://www.cnblogs.com/JoannaQ/p/3393530.html
Copyright © 2011-2022 走看看