define function of Seajs

Source: Internet
Author: User
Tags define function

The seajs.config is used to configure the SEAJS.

In the CMD specification, a module is a file. The code is written in the following format: define (Factory);
Define accepts the factory parameter, factory can be a function, or it can be an object or a string.
When factory is an object, a string, the interface that represents the module is the object, the string.

When factory is a function, the representation is the construction method of the module.
Used to define the module. Seajs respected a module of a file, following a unified notation:

Define (function (Require, exports, module) {
Module code
});

You can also manually specify the module ID and dependencies, require, exports, and module three parameters can be omitted as appropriate, the specific usage is as follows.

Define can also accept more than two parameters. The string ID represents the module identifier, and the array deps is module dependent. In the development phase, the handwriting ID and Deps parameters are not recommended because these two parameters can be generated automatically by tools during the build phase.

Define (ID, deps, Factory)
Define (' Hello ', [' jquery '], function (require, exports, module) {
Module code
});

Require is a method that accepts a module identity as a unique parameter that is used to obtain the interfaces provided by other modules.

Define (function (require, exports) {

Get the interface for module a
var a = require ('./a ');

Method of calling Module a
A.dosomething ();

});

These rules must be followed when writing module code. In fact, as long as the require as a grammatical keyword is good. The parameter value of the require must be the direct amount of the string. Do not rename the Require function, or reassign a value to require in any scope. The first parameter of the module factory construction method must be named require.

Exports is an object used to provide a module interface to the outside.

Define (function (require, exports) {

Provide Foo property externally
Exports.foo = ' Bar ';

DoSomething Methods for external delivery
exports.dosomething = function () {};

});

In addition to adding members to the exports object, you can also use return to provide an interface directly to the outside.

Define (function (require) {

Provide interface directly via return
return {
Foo: ' Bar ',
Dosomething:function () {};
};

});

If the return statement is a unique code in a module, it can also be simplified to:

Define ({
Foo: ' Bar ',
Dosomething:function () {};
});

Tip: Exports is just a reference to Module.exports. The value of Module.exports is not changed when the exports is re-assigned within factory. The assignment to exports is therefore invalid and cannot be used to change the module interface.

The module is an object that stores some of the properties and methods associated with the current module.

Unique identification of the Module.id module.

Define (' id ', [], function (require, exports, module) {

Module code

});

In the above code, the first parameter of define is the module identifier.

Module.exports the interface provided externally by the current module.

The exports parameter passed to the factory constructor method is a reference to the Module.exports object. Interfaces are provided only through the exports parameter, and sometimes not all of the developer's needs are met. For example, when the interface of a module is an instance of a class, it needs to be implemented by Module.exports:

Define (function (Require, exports, module) {

Exports is a reference to Module.exports
Console.log (Module.exports = = = exports); True

Re-assigning module.exports to a value
Module.exports = new SomeClass ();

Exports no longer equals module.exports
Console.log (Module.exports = = = exports); False

});

Seajs define function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.