Home  >  Article  >  Web Front-end  >  How does the javascript module loader work?

How does the javascript module loader work?

php中世界最好的语言
php中世界最好的语言Original
2018-03-17 11:48:471591browse

This time I will bring you javascriptHow does the module loader run? What are the precautions for running the javascript module loader?. Here is a practical case. Let’s take a look. .

Definition

var MyModules = (function Manager() {
  var modules = {};
  function define (name, deps, impl) {
    for(var j = 0, length = deps.length; j < length; j++){
      deps[j] = modules[deps[j]];
    }
    modules[name] = impl.apply(impl, deps);
  }
  function get (name) {
    return modules[name];
  }
  return {
    define: define,
    get: get
  }
})();

Use

MyModules.define('test1', [], function() {
  function hello(name) {
    console.log(name);
  }
  
  return {
    hello: hello
  }
});
MyModules.define('test2', ['test1'], function(test1) {
  function age(name, age) {
    console.log(test1.hello(name));
    console.log(age);
  }
  
  return {
    age: age
  }
});
MyModules.get('test2').age('mumu', '27');

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Use js to quickly get the address of the image in the html page

Set cookie expiration to automatically update and How to automatically obtain the option overlay of


##

The above is the detailed content of How does the javascript module loader work?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn