269N/A<p>
269N/A`YUI.add()` is a static method that registers a reusable module&mdash;essentially, it adds a module to the set of modules available to be attached to a YUI instance via the `use()` method.
851N/A</p>
269N/A
1625N/A<p>
269N/ADefining a reusable YUI module is as simple as providing a name and a callback function to `YUI.add()`.
269N/A</p>
919N/A
919N/A```
919N/AYUI.add('my-module', function (Y) {
919N/A // Write your module code here, and make your module available on the Y
919N/A // object if desired.
919N/A Y.MyModule = {
919N/A sayHello: function () {
919N/A console.log('Hello!');
919N/A }
919N/A };
919N/A});
919N/A```
919N/A
919N/A<p>
919N/ANote that there are no parentheses after `YUI` when calling `YUI.add()` as there are when calling `YUI().use()`. This is because `add()` is a static method of the global `YUI` object, not a method on a specific YUI instance. Modules are registered globally via `add()` and are later attached to a specific YUI instance via `use()`.
919N/A</p>
919N/A
269N/A<p>
269N/AThe `add()` method accepts two optional arguments after the callback function: a module version string and a config object. The most useful option in the config object is `requires`, which allows you to specify an array of other YUI modules that your module requires. YUI will then be sure to load these dependencies before executing your module.
269N/A</p>
269N/A
493N/A```
269N/AYUI.add('my-module', function (Y) {
269N/A // ...
1634N/A}, '0.0.1', {
269N/A requires: ['node', 'event']
911N/A});
1634N/A```
1634N/A
1634N/A<p>
911N/AAfter your module has been added via `YUI.add()`, you can specify its name in a `use()` call to attach it to a YUI instance.
269N/A</p>
1215N/A
1665N/A```
1179N/AYUI().use('my-module', function (Y) {
1625N/A // The Y instance here is the same Y instance that was passed into
1179N/A // my-module's add() callback, so the Y.MyModule object that was created
851N/A // there is now available here as well.
1625N/A Y.MyModule.sayHello();
1625N/A});
1625N/A```
269N/A
269N/A<p>
269N/AA module's `add()` callback isn't executed until that module is attached to a YUI instance via `use()`. Each time a module is attached via `use()`, the module's `add()` callback will be executed, and will receive as an argument the same YUI instance that will later be passed to the `use()` callback.
970N/A</p>
970N/A