我何时应该使用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. 要求:如果您只想加载和使用东西。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值