A detailed analysis of scope problems in Nodejs

Source: Internet
Author: User
Tags script tag

There are global scopes and function scopes in JS, and their scopes in Nodejs are divided into global scope and module scope.

JS Scope:

We used to learn JS when our global object is window, such as:

Vara = 10;
Console.log (WINDOW.A);

The global variable we define defaults to adding a property or method to the window.

Functionfn () {
Varnum = 22;
}
Console.log (num);

The error, because NUM is defined in the function, outside of the function can not access the variables inside the function, although it is possible to access the variables in the function through the closure, but this is not the point I want to say, this is the function scope.

Well, today's point is, what is the scope of the Nodejs?

First I defined a global variable in file 1.js, which is defined by global.

Global.web = "Nodejs";

Then I use 2.js to refer to 1.js files

Require ('./1.js ');

In the Nodejs has no html,dom, and BOM, naturally there is no script tag, in node if you need to introduce a file, you need to use the Require method to load the file, which write the corresponding file path, generally we write relative path, The relative path needs to be added./If the absolute path is used/. Next we visit the global variable in 2.js.

Console.log (Global.web); Nodejs

Ok

This is simpler, at first I also said that there is a module scope in the Nodejs, and what is the module scope?

When it comes to module scopes, we have to mention modular development, if you have experience in modular development you must have had a headache for variable names, because if you write code that is not standard with your colleagues, then if you have the same variable name in your code, then the following variable will overwrite the previous variable. In JS we can solve this problem by function, and in Nodejs, in addition to the scope of the function, Nodejs also provides a module scope, each file is a module, which means that each file has its own scope, If you do not define a variable through global, another file cannot access the variable for that file. Of course, if you want to access a variable for another file in one file, there is a way beyond global, which we'll talk about later.

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.