When should I use require() and when to use define()?

后端 未结 5 1832
抹茶落季
抹茶落季 2020-11-30 16:19

I have being playing around with requirejs for the last few days. I am trying to understand the differences between define and require.

Define seems to allow for mo

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 16:47

    "define" method for facilitating module definition and "require" method for handling dependency loading

    define is used to define named or unnamed modules based on the proposal using the following signature:

    define(
    module_id /*optional*/, 
    [dependencies] /*optional*/, 
    definition function /*function for instantiating the module or object*/
    );
    

    require on the other hand is typically used to load code in a top-level JavaScript file or within a module should you wish to dynamically fetch dependencies

    Refer to https://addyosmani.com/writing-modular-js/ for more information.

提交回复
热议问题