What is the difference between define
and require
, and when should I use each of them? I have read different answers on Stack Overflow, but I still haven't been able to understand.
For example, if this was on main.js
(config file require points to), what is the difference?
define(["jquery"], function($) {
do something with $
});
require(["jquery"], function($) {
do something with $
});
Is $/jQ
guaranteed to be loaded and ready in both?
They do the same thing internally. But...... you should define your entry point of your app using require
and define the rest of the modules using define
. I find that this keeps it clear what role the current module you're looking at is actually playing in terms of your whole app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With