JS simple module loading principle and advice

I want to learn the core principle of modular loader. Now it can dynamically load files and prevent overloading, but at present, it is stuck in the callback function to receive parameters. Please give me some advice. Thank you.

for example:

A.js
require([""],function(){
    // 
});

B.js
define(function(){
    // A
})

I understand that the callback function is executed after dynamically loading the module, but I don"t know how this parameter is passed, how the callback function gets the return result of the module, and what is the intermediate key between them? If you can give a simple demo will be very grateful, text description of the principle is also OK, thank you.

Mar.18,2021

define implementation:

// 
const moduleResult = {}

function define(module){
    moduleResult[filepath] = module()
}

require implementation:

function require(dependenceList, callback){
    callback(...dependenceList.map(dependence => moduleResult[dependence]))
}

there is only principle, and nothing else is considered


just call the second parameter normally.

function require(list, callback) {
    // list
    // const result1 = eval('xx1.js')
    // const result2 = eval('xx2.js')
    callback(result1, result2)
}

take a look at my article seajs and requirejs Modular Development

Menu