Home > Web Front-end > JS Tutorial > Detailed explanation of JavaScript module loader through code examples

Detailed explanation of JavaScript module loader through code examples

亚连
Release: 2018-06-01 15:23:42
Original
1307 people have browsed it

This article uses code examples to give you a detailed analysis of the relevant knowledge points of the javascript basic content module loader, let’s learn together.

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
  }
})();
Copy after login

Use

MyModules.define(&#39;test1&#39;, [], function() {
  function hello(name) {
    console.log(name);
  }
  
  return {
    hello: hello
  }
});

MyModules.define(&#39;test2&#39;, [&#39;test1&#39;], function(test1) {
  function age(name, age) {
    console.log(test1.hello(name));
    console.log(age);
  }
  
  return {
    age: age
  }
});

MyModules.get(&#39;test2&#39;).age(&#39;mumu&#39;, &#39;27&#39;);
Copy after login

The above is what I compiled For everyone, I hope it will be helpful to everyone in the future.

Related articles:

Two ways for Vue single-page application to reference separate style files

JS and Canvas implementation Picture preview compression and upload function

postman json springmvc test batch adding instance

The above is the detailed content of Detailed explanation of JavaScript module loader through code examples. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template