我何時應該使用require()以及何時使用define()?

本文翻譯自:When should I use require() and when to use define()?

I have being playing around with requirejs for the last few days. 過去幾天我一直在玩requirejs。 I am trying to understand the differences between define and require. 我試圖理解define和require之間的區別。

Define seems to allow for module separation and allow for dependency ordering to be adhere. 定義似乎允許模塊分離並允許遵守依賴性排序。 But it downloads all the files it needs to begin with. 但它會下載所需的所有文件。 Whilst require only loads what you need when you need it. 雖然只需要在您需要時加載您需要的東西。

Can these two be used together and for what purposes should each of them be used? 這兩者可以一起使用,是否應該使用它們?


#1樓

參考:https://stackoom.com/question/dtMU/我何時應該使用require-以及何時使用define


#2樓

From the require.js source code (line 1902): 從require.js 源代碼 (第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.
 */

The define() function accepts two optional parameters (a string that represent a module ID and an array of required modules) and one required parameter (a factory method). define()函數接受兩個可選參數(表示模塊ID的字符串和所需模塊的數組)和一個必需參數(工廠方法)。

The return of the factory method MUST return the implementation for your module (in the same way that the Module Pattern does). 返回工廠方法必須返回模塊的實現(與模塊模式相同)。

The require() function doesn't have to return the implementation of a new module. require()函數不必返回新模塊的實現。

Using define() you are asking something like "run the function that I am passing as a parameter and assign whatever returns to the ID that I am passing but, before, check that these dependencies are loaded" . 使用define()你會問一些類似“運行我作爲參數傳遞的函數並將任何返回值分配給我傳遞的ID,但之前,請檢查是否已加載這些依賴項”

Using require() you are saying something like "the function that I pass has the following dependencies, check that these dependencies are loaded before running it" . 使用require()你會說“我傳遞的函數具有以下依賴關係,請在運行之前檢查這些依賴項是否已加載”

The require() function is where you use your defined modules, in order to be sure that the modules are defined, but you are not defining new modules there. require()函數是您使用已定義模塊的地方,以確保模塊已定義,但您沒有在那裏定義新模塊。


#3樓

"define" method for facilitating module definition and "require" method for handling dependency loading 用於促進模塊定義的“define”方法和用於處理依賴性加載的“require”方法

define is used to define named or unnamed modules based on the proposal using the following signature: define用於根據提議使用以下簽名定義命名或未命名的模塊:

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

require on the other hand is typically used to load code in a top-level JavaScript file or within a module should you wish to dynamically fetch dependencies 另一方面,require通常用於在頂級JavaScript文件或模塊內加載代碼,如果您希望動態獲取依賴項

Refer to https://addyosmani.com/writing-modular-js/ for more information. 有關更多信息,請參閱https://addyosmani.com/writing-modular-js/


#4樓

require() and define() both used to load dependencies.There is a major difference between these two method. require()和define()都用於加載依賴項。這兩種方法之間存在重大差異。

Its very Simple Guys 它非常簡單

Require() : Method is used to run immediate functionalities. Require():方法用於運行直接功能。 define() : Method is used to define modules for use in multiple locations(reuse). define():方法用於定義在多個位置使用的模塊(重用)。


#5樓

General rules: 通用規則:

  1. You use define when you want to define a module that will be reused 當您想要定義將被重用的模塊時,可以使用define

  2. You use require to simply load a dependency 您使用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 }); 

Hope this helps you. 希望這對你有所幫助。


#6樓

With define you register a module in require.js that you can then depend on in other module definitions or require statements. 使用define ,您可以在require.js中註冊一個模塊,然後您可以依賴於其他模塊定義或require語句。 With require you "just" load/use a module or javascript file that can be loaded by require.js. 有了require你只需“加載”/使用require.js可以加載的模塊或javascript文件。 For examples have a look at the documentation 有關示例,請查看文檔

My rule of thumb: 我的經驗法則:

  • Define: If you want to declare a module other parts of your application will depend on. 定義:如果要聲明模塊,應用程序的其他部分將依賴於。

  • Require: If you just want to load and use stuff. 要求:如果您只想加載和使用東西。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章