node. js in module, require

Source: Internet
Author: User

In php,c++, there is the concept of namespaces, namespaces are mainly used to solve the introduction of file existence function, class, variable name of the problem, in node. js, there is no namespace so complex concept, in node, there is the concept of module, that is, the functional code is put together. It is then introduced and used in this document. This does not occur in the function name, variable names problem, in the introduction of the module, are registered in the form of import. Look at the following example:

// Circle.js var pi = 3.14; // calculate the perimeter of a circle function (r) {        return 2 * pi * r;} // calculate the area of a circle function (r) {        return pi * R * r;} ~    
var circle = require ('./circle '); Console.log (Circle.circle (2)); Console.log (Circle.area ( 3)); ~                                  

1, so the above pi in the introduction of the file is not useful, it only in the Circle.js local effective. Then exports. Set the properties, and then you can use the callback function with these two functions

Attention:

1, if the directory is introduced, the default is to introduce the directory under the Index.js this file, or to specify the module name, you can omit the. js suffix.

2, using require to introduce the module, this is an IO synchronous operation, so it is best to introduce at the beginning of the file, if the IO volume is large, the introduction, there will be a series of problems such as blocking. Performance degradation.

node. js in module, 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.