1. Global and local scopes

  • Scope: Specifies the scope within which a variable or function can be used. there are two types of scope, global scope and local scope
  • Global scope: written directly inscriptThe code in the tag, or in a single JS file, is global. The global scope is opened when the page is created and destroyed when the page is closed. In front-end JS, there is a global object window (representing a browser window created by the browser) in the global scope that can be used directly. In Node, there is a global object, Global.
  • Local scope: inside a function is a local scope. The name of this code only works inside a function. The function scope is created when a function is called, and the function scope is destroyed after the function is executed. A new function scope is created each time a function is called, and they are independent of each other.

2.Node.js scope

  • In Node, each JS file is a separate scope, because Node’s modularity encapsulates the code in each file within a function, unlike in browser development, where var is used Js files and JS files share scope. However, in Node.js, each JS file is independent and the scope is not shared, so variables in each file in Node are private.

    Write the following code:

    var num = 10;
    Copy the code

    Function (exports,require,module,__filename,__dirname){var num=10}

    var num = 10; console.log(arguments); / / print the information of the current module console. The log (the arguments. The callee. ToString ()); // Print the function bodyCopy the code

  • All user-written code is automatically wrapped in a function that takes five arguments, so we can use five arguments inside the function

    Exports and require are passed as arguments, and if the argument is a reference type, changes in one place will happen elsewhere.

  • Expose module data: Let other scopes use values from this scope

    With exports exposure, the input value is a copy of the output value, and once a value is exported, changes within the module do not affect the value (just normal values, if it is a reference value, it will be affected).

    Exports exposes data, which must be retrieved through objects when imported, and can expose multiple data

    Let a = 100 exports. A = a//exports is an object to which an attribute a is added, and the value of a is the value of the variable in this scopeCopy the code

    Exports exposes data using module.exports

    Module. exports exposes data, exposes what it imports, exposes only one thing (class, constructor, etc.)

    function Student(name,age,sex){this.name = name; this.age = age; This. Sex = sex} module. Exports = Student // let stu = require("./a.js") var stu = new stu(" xiaoming ",18,"male") Var xiaoming = new stu.Student("",18,"")Copy the code

    Expose data using global variables

    	var username = 'xiaoming
       	global.name = username
    Copy the code

    You can use name instead of global

3. Commonality of modules

  • All modularity has one thing in common. The function of modules is to put code into a separate function
  • Variables defined using var in a module are local variables
  • Functions defined in modules are also local
  • Modules have a module object that contains moduleName,exports
  • If a module needs to expose methods or properties for external use, it adds properties to exports objects
  • Import a module using require(‘moduleName’), which returns an exports object that is a module object

4. Node global scope and global functions

  • Customize global variables or methods
	global.aa = 10
	global.add = (a,b) => a +b;
Copy the code
  • Built-in global methods

    ① The Timout function and clearTimeout function set and clear delay timer. Unlike the browser delay timer, the first argument to the node delay method cannot be a string

    setTimeout(function () {
      console.log(1111)
    }, 2000)
    Copy the code

    ② setInterval and clearInterval set and clearInterval (cycle) timers

    setInterval(function () {
      console.log(1111)
    }, 2000)
    Copy the code

    The setImmediate function is similar to the clearImmediate mediate function, but the setImmediate function executes at the end of the current time poll

    setImmediate(() => {
      console.log(123)
    })
    Copy the code

5. Process of the process

  • Process is a global object property in Node that can be used in any module. This object has properties, methods, and events that can be used to get various information about the Node.js application, the user running the application, and the running environment

  • The process.argv property returns an array of command line arguments that started the Node.js process, or, if no arguments are passed, an array of the current Node file path as the first entry and the path to the running file as the second.

  • The execPath property, the absolute pathname of the executable that starts the Node.js process, is the first value in the process.argv array.

  • The process.env attribute returns an object containing information about the user’s environment. Adding a new attribute to process.env converts the value of the attribute to a string

  • The CWD method returns the current working directory path of the Node.js process

  • NextTick method

    The first parameter is mandatory and is optional after the second parameter of the callback function

    The process.nexttick () method adds the callback to the queue at the next point in time. This queue is completely exhausted after the current operation on the JavaScript stack completes and before the event loop is allowed to continue. If you want to recursively call process.nexttick (), you can create an infinite loop

    Console. The log (' start '); // 1 process.nexttick (() => {console.log(' callback at next point in time '); / / 3}); The console. The log (' '); / / 2Copy the code
    Process.nexttick (() => {console.log(' next point callback ')}) setImmediate(() => {console.log("setImediate")}) setTimeout(() => {console.log("setImediate")})  { console.log("setTimeout") })Copy the code