35

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!

Andrew Ferrier
  • 16,664
  • 13
  • 47
  • 76
Simple-Solution
  • 4,209
  • 12
  • 47
  • 66
  • 1
    Another good link: [Dojo require() vs. define()](http://g00glen00b.be/dojo-require-vs-define/). "define()" provides a return value and lazily loads the dependencies. It's used to define your own custom modules and the dependencies it needs. "require()"'s main use is importing modules. Both are synchronous; only require() is non-lazy. – FoggyDay May 03 '15 at 00:19

1 Answers1

64

require and define are part of the asynchronous module definition (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.

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.

AMD API

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

Craig Swing
  • 8,152
  • 2
  • 30
  • 43
  • Discovered a symptom of using the wrong one is the callback returning immediately before the code runs... http://stackoverflow.com/questions/5590618/requirejs-calling-callbacks-before-dependencies-loaded-resolved?rq=1 – prototype Apr 09 '14 at 04:26
  • when you are defining a new module and you need another module which you had already created , you can use define to require that module – AhammadaliPK Jun 07 '17 at 12:29