Requirejs define Require

Source: Internet
Author: User

Original address: http://blog.csdn.net/xiaogou56a/article/details/21340213

Define used to define modules

Require used to load modules

1

Because defining a module may depend on other modules, and of course the simplest case is to not rely on other modules, then you can write:

[JavaScript]View Plaincopy
    1. Inside file My/shirt.js:
    2. Define ({
    3. Color: "Black",
    4. Size: "Unisize"
    5. });


Official explanation: If The module does not has any dependencies, and it's just a collection of name/value pairs, then just pass an obje CT literal to define ():

2

Define a module, or you may need to do some setup work first, assuming it does not depend on other modules, you can write this:

[JavaScript]View Plaincopy
    1. //my/shirt.js now does setup  work  
    2. //before returning its module  definition.  
    3. define (function  ()  {   
    4.     //do setup work here   
    5.   
    6.     return  {  
    7.         color: < Span class= "string", "Black",   
    8.         size:   "unisize" &NBSP;&NBSP;
    9.     }   
    10. });   


Official explanation: If The module does not has dependencies, but needs to use a function to do some setup work, then define itself, pass A function to define ():

3

Defining a module can be complex, depending on a few other modules, and requires some setup work, so this is the time to write:

[JavaScript]View Plaincopy
  1. My/shirt.js now has some dependencies, a cart and inventory
  2. module in the same directory as Shirt.js
  3. define (["./cart", "./inventory"], function (cart, inventory) {
  4. //return An object to define the "My/shirt" module.
  5. return {
  6. Color: "Blue",
  7. Size: "large",
  8. AddToCart: function () {
  9. Inventory.decrement (this);
  10. Cart.add (this);
  11. }
  12. }
  13. }
  14. );

The dependent module is passed in to that function once as a parameter.

See official explanation: If The module has dependencies, the first argument should is an array of dependency names, and the second argument s Hould be a definition function. The function would be called to define the module once all dependencies has loaded. The function should return an object that defines the module. The dependencies is passed to the definition function as function arguments, and listed in the same order as the The dependency array:

4

Define a Module with a Name

[JavaScript]View Plaincopy
    1. Explicitly defines the "Foo/title" module:
    2. Define ("Foo/title",
    3. ["My/cart", "My/inventory"],
    4. function (cart, inventory) {
    5. //define Foo/title object in here.
    6. }
    7. );

I believe you understand it at a glance, but the knowledge inside it can be a good time to look at the official documentation, perhaps very interesting oh.

There are many other situations, but remember the essence, define is used to define the module, require is used to load the module, the entire library development considerations are more, such as: In order to be compatible with the previous code, in order to adapt to the use of certain libraries, the use of certain conversion tools, meta-programming applications, and so on. As long as we grasp the essence, understand the meaning, the specific format of reference official website, as long as others use, it is certainly legitimate, there must be a source, today ends.

Requirejs define Require

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.