RequireJs - Define vs Require

后端 未结 2 684
慢半拍i
慢半拍i 2020-12-12 18:46

For modules I don\'t return an object I have been using require instead of define. For example say I have the following jQuery plugin (jquery.my-plugin.js):

         


        
2条回答
  •  长情又很酷
    2020-12-12 19:45

    Essentially, when you use require you are saying "i want this, but i want all its dependencies too". So in the example below, we're requiring A, but require will search for all dependencies and ensure they are loaded before continuing.

    require(['a'], function(a) {
        // b, c, d, e will be loaded
    });
    
    // File A
    define(['b','c','d','e'], function() {
        return this;
    });
    

    General rule of thumb is you use define when you want to define a module that will be reused by your application and you use require to simply load a dependency.

提交回复
热议问题