What do I need to define the define () method described in the CommonJS specification?

I understand how things like matching names and Module Patterns can cope with issues related to global leakage .

I also fully see the value of resource dependency management, which is provided for in the protocol require()described in the CommonJS Specification .

However, I'm stupefied in favor of using and assigning AMD features define().

CommonJS signature for definition:

define(id?, dependencies?, factory);

Additionally...

Firstly, it “seemed” to be somehow another plugin shell ... until I began to see how people use it together with the module template.

So my questions are:

  • What does the protocol define() outlined in the CommonJS specification buy me?
  • Is it somehow more eloquent?
  • Does this mean replacing a modular template?
  • Somehow faster?
  • If so, why?
+2
source share
1 answer
// main.js
require("foo.js", function(foo) {
    console.log(foo === 42); // true
});

//foo.js

/*
define(42);

define({
   "foo": "bar"
});

define(["bar.js"], function(bar) {
    return bar.foo;
});
*/

define(function() {
     return 42;
});

Define is a great way to pass modular objects back without relying on the global scope.

The specific definition API is library to library dependent.

, , , . , , . , .

(, ).

require define, .

define requireJS API

+2

All Articles