Modular development Seajs

Source: Internet
Author: User

1, first download the Seajs, and then in our project to create a new folder named Sea (the name can be arbitrarily), the download of the Sea.js file to the inside, and finally in the HTML document with the script tag introduced can be used.

2, in the modular, a JS file is a module, JS folder All JS files are modules. Of course, the ordinary JS file into a module, to do a change. Use the Define () method to define the JS file as a module. The define method takes a function as a parameter , and the function has three parameters, as follows.

Define (function(require,exports,module) {//    require  introduces the dependency of the module;//      Exports  exposes the method of the module, provides an external interface;//Module   means the modules themselves;     Note that these two parameter names cannot be changed, and the order of the parameters cannot change.     function  Show () {        alert (1);    }     = Show;})

3, how to call the module in the HTML document, with the Seajs.use method, it accepts two parameters, one is to introduce the module, and one is to introduce a successful callback function. The callback function has a parameter that points to the exports in the module that we introduced, so you can use this parameter to expose the method to the module.

<script>    seajs.use (function(ex) {        //  Note file path,./ Represents the folder in which the current HTML document resides         ex.show        (); // The ex parameter points to the exports in the module     })</script>

4, if a module relies on another module, it is necessary to use the Require parameter, which takes a parameter, the path of the introduced module, the return value is also pointed to the introduction of the module exports, so can be called by the return value of the module exposed methods. Change the module above 2 to introduce dependencies.

Define (function(require,exports,module) {//    require  introduces the dependency of the module;//      Exports  exposes the method of the module, provides an external interface;//Module   means the modules themselves;     Note that these two parameter names cannot be changed, and the order of the parameters cannot change.

var external = Reqiure ("./moudules3.js"), var external = reqiure ("./moudules3.js") Check the wrong for a long time.
function Show () {         alert (external.a);   
  

  = Show;})

Modular development Seajs

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.