Custom Module Guidelines
Choosing Between the define Object and require Function
Use the define Object for entry point scripts or when creating new modules. Use the require Function to load and use existing modules.
The require Function can provide a performance advantage because it loads dependencies as needed. The define Object imports all dependencies up front.
The following table summarizes when to use the define Object and the require Function:
|
Object/Function |
When to Use |
Caveat |
|---|---|---|
|
define Object |
|
If you use define, you won't get the performance boost of progressive loading. |
|
require Function |
|
If you use require, you might need to import define first to get the right context. For example, to import a custom module by relative path:
|
Using Third-Party Libraries with Custom Modules
You can use third-party libraries by defining the library as a custom module, setting a global identifier to reference it, and setting it up as a dependency. For more examples of using third-party and non-AMD-compatible libraries, see Use an AMD-Compatible Library and Use a Third-Party JavaScript Library.