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
What is the main difference between require() and define() function in dojo and when would we use either?
提问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
require
and define
are part of the asynchronous module definition (AMD) API.
require
并且define
是异步模块定义 (AMD) API 的一部分。
You use define
to define a module that can be consumed by other code. Generally, define
will 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 require
when you are not defining a module, but you require modules that have been defined. Generally, require
will 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