CommonJs and AMD are like dependent injections?

I just touch concepts dependency injection,

and then I think CommonJs and AMD are related dependency injection.

Because when defining a module under any standard, you must first load its dependent module.

The module you defined should only provide an abstract API, in less detail

I'm right? Did I miss something?

+4
source share
2 answers

It can be used as such with some work, but in fact it is not.

. . , , .

, , , , .

- , config. . dependency-injection, . , in

require.config({
    paths: {
        modernizr: '//cdnjs.cloudflare.com/ajax/libs/modernizr/2.7.1/modernizr.min'
    }
});

, config.

+1

:

AMD JavaScript:

  • #include - C
  • import Java
  • require NodeJS

, , DI - - Singleton.

:

define('a', function () {
  return {
    doX: function () { ... }
  };
});

define('b', ['a'],  function (a) {
  return {
    doY: function () { 
      a.doX(); 
    }
  };
});

, a b, a - Singleton - , AMD , , .

:

define('A', function () {
  function A(options) {  // some constructor/factory method
     return {
       doX: function () {...}
     }
  }

  return A;
});

define('b', ['A'],  function (A) {

    if (<some condition>) {
      var customOptions = {<some options>}
      var a1 = new A(customOptions);

      return {
         doY: function () {
            a1.doX();
         }
      };
    }

    var defaultOptions = {<default>}
    var a2 = new A(defaultOptions);
    return {
      doY: function () {
         a2.doY();
      }
    }
});

A, / factory .

, , , , . , , , - Singleton.

0

All Articles