Use mini-define to implement modular Management of front-end code _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to use mini-define to implement modular Management of front-end code. It is a very good article. We recommend it to partners who need it. Mini-define

A simple front-end modular framework based on require. If you don't want to spend time learning require. js or look at the long cmd/amd specifications, then this mini-define is your good choice. If you have used sea. js or require. js before, mini-define is more efficient, lightweight, and easy to use. Project address: github

Usage

First define the module

Definition Module

I. Use the define function to define a module

1.1 depending on whether there is dependency, there are two cases:

1.1.1: no dependent Module

The Code is as follows:


Define ('id', function (){
// Put your code here
});

1.1.2: Dependent modules

The Code is as follows:


Define ('id', ['modea ', 'modeb'], function (A, B ){
// Put your code here
});

1.2 if you want to return the processing result for external use, there are two situations:

1.2.1 returned objects:

The Code is as follows:


Define ('id', function (){
Return {
// Put your code here
}
});

1.2.2 no objects returned

The Code is as follows:


Define ('id', function (){
// Put your code here
});

Ii. Use the require () function to call the module.

2.1 depending on the number of requested modules, there are two possible cases:

2.1.1. Call a single module

Require ('modeid ')

2.1.2. Call multiple modules
Require (['modea ', 'modeb']);
2.2 Based on whether callback processing exists, there are two possible scenarios:

2.2.1 callback Handler

The Code is as follows:


Require ('modeid', function (mode ){
// Put your code here
});

Require (['modea ', 'modeb'], function (A, B ){
// Put your code here
});

2.2.2 no callback Processing
Require ('modeid ');
Reference the required modules on the index.html page.

The Code is as follows:



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.