【问题标题】:When should I use require() and when to use define()?什么时候应该使用require(),什么时候应该使用define()?
【发布时间】:2012-03-19 10:33:18
【问题描述】:

过去几天我一直在玩 requirejs。我正在尝试了解定义和要求之间的区别。

Define 似乎允许模块分离并允许遵守依赖顺序。但它会下载它开始时需要的所有文件。而 require 仅在您需要时加载您需要的内容。

这两者可以一起使用吗?分别用于什么目的?

【问题讨论】:

    标签: require requirejs


    【解决方案1】:

    使用define,您可以在 require.js 中注册一个模块,然后您可以在其他模块定义或 require 语句中依赖该模块。 使用require,您“只需”加载/使用可由 require.js 加载的模块或 javascript 文件。 例如看看documentation

    我的经验法则:

    • 定义:如果你想声明一个模块,你的应用程序的其他部分将会依赖。

    • 要求:如果你只是想加载和使用东西。

    【讨论】:

      【解决方案2】:

      来自 require.js source code(第 1902 行):

      /**
       * The function that handles definitions of modules. Differs from
       * require() in that a string for the module should be the first argument,
       * and the function to execute after dependencies are loaded should
       * return a value to define the module corresponding to the first argument's
       * name.
       */
      

      define() 函数接受两个可选参数(代表模块 ID 的字符串和所需模块的数组)和一个必需参数(工厂方法)。

      工厂方法的返回必须返回您的模块的实现(与Module Pattern 一样)。

      require() 函数不必返回新模块的实现。

      使用 define() 你在问类似 “运行我作为参数传递的函数并将任何返回值分配给我传递的 ID,但在此之前,请检查已加载这些依赖项”

      使用 require() 你在说类似 “我传递的函数具有以下依赖项,请在运行之前检查这些依赖项是否已加载”。 p>

      require() 函数是您使用已定义模块的地方,以确保已定义模块,但您并未在此处定义新模块。

      【讨论】:

      • 在定义的模块内部还是外部使用 require 有什么区别吗?如果在模块内部使用,为什么不直接在模块定义中设置需求而不是使用require呢?
      • 为什么这个答案和我在这里看到的 requirejs.org/docs/api.html#deffunc 如此不同??
      • @Petri,听起来您正在看到 RequireJS 的第 2 版异步加载模块的行为。 “RequireJS 2.0 不会执行模块的工厂函数(传递给 define() 的函数),直到有一个 require([]) 调用要求它,或者依赖它的东西。” github.com/jrburke/requirejs/wiki/…
      【解决方案3】:

      一般规则:

      1. 当你想定义一个将被重用的模块时,你使用define

      2. 您使用 require 来简单地加载依赖项

        //sample1.js file : module definition 
        define(function() {
              var sample1 = {};
              //do your stuff
             return sample1;
         });
        
        //sample2.js file : module definition and also has a dependency on jQuery and sample1.js
        define(['jquery', 'sample1'], function($,sample1) {
            var sample2 = {
                getSample1:sample1.getSomeData();
            };
            var selectSomeElement = $('#someElementId');
            //do your stuff....
            return sample2;
        });
        
        //calling in any file (mainly in entry file)
        require(['sample2'], function(sample2) {
            // sample1 will be loaded also
        });
        

      希望对你有所帮助。

      【讨论】:

        【解决方案4】:

        便于模块定义的“define”方法 和 处理依赖加载的“require”方法

        define 用于根据使用以下签名的提案定义命名或未命名模块:

        define(
        module_id /*optional*/, 
        [dependencies] /*optional*/, 
        definition function /*function for instantiating the module or object*/
        );
        

        另一方面,require 通常用于在顶级 JavaScript 文件或模块中加载代码,如果您希望动态获取依赖项

        请参阅https://addyosmani.com/writing-modular-js/ 了解更多信息。

        【讨论】:

          【解决方案5】:

          require()和define()都是用来加载依赖的,这两种方法有很大的不同。

          很简单的人

          Require() :方法用于运行即时功能。 define() : 方法用于定义在多个位置使用的模块(重用)。

          【讨论】:

            猜你喜欢
            • 2010-12-24
            • 2023-04-02
            • 2011-04-15
            • 2017-04-10
            • 2018-05-12
            • 2018-12-11
            • 1970-01-01
            • 2022-09-28
            • 2021-09-07
            相关资源
            最近更新 更多