javascript dojo 中的 require() 和 define() 函数之间的主要区别是什么?我们什么时候使用它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11559270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:40:27  来源:igfitidea点击:

What is the main difference between require() and define() function in dojo and when would we use either?

javascriptdojojs-amdibm-mobilefirst

提问by Simple-Solution

I am new to learning dojo and I have come across the require()and define()functions and I can not get my head around either of them. Also, when would I use either of them? A small demo or example would be beneficial. Many Thanks!

我是学习道场的新手,我遇到了require()define()功能,我无法理解它们中的任何一个。另外,我什么时候会使用它们中的任何一个?一个小的演示或示例将是有益的。非常感谢!

回答by Craig Swing

requireand defineare part of the asynchronous module definition (AMD) API.

require并且define是异步模块定义 (AMD) API 的一部分。

You use defineto define a module that can be consumed by other code. Generally, definewill be used in a javascript file. The javascript file is defining a module. All Dojo files use define.

define用来定义一个可以被其他代码使用的模块。一般define会在javascript文件中使用。javascript 文件正在定义一个模块。所有 Dojo 文件都使用定义。

You use requirewhen you are not defining a module, but you require modules that have been defined. Generally, requirewill be used in HTML pages. The HTML page is not a module, but requires modules to present the page to the user.

您可以使用require,当你不能定义一个模块,但你需要已定义的模块。一般情况下,require会在HTML页面中使用。HTML 页面不是模块,但需要模块才能将页面呈现给用户。

AMD API

AMD API

https://github.com/amdjs/amdjs-api/wiki/AMD

https://github.com/amdjs/amdjs-api/wiki/AMD