Summary of the differences between global objects in nodejs and browsers

Summary of the differences between global objects in nodejs and browsers

In Node.js, a .js file is a complete scope (module). Therefore, the variables declared by var are only valid in the current .js file, not globally. The global object is independent of all .js (module).

The top-level global object in the browser is window, and variables declared with var are bound to the window object by default.

1. Definition of global object

Concept: An object that can be accessed from anywhere in the program is called a global object. The properties of an object are called global variables.

2. Summary of global variables in NodeJS

Here we summarize what global variables we commonly use in nodejs

2.1 Buffer Class

Buffer, which we can also call "buffer", has the function of opening up an area in the memory to store binary data.

2.2 __dirname

__dirname, returns the absolute path of the folder (directory) where the current module file is located after parsing.

Remember that __dirname is not really a global variable.

2.3 __filename

__filename, returns the absolute path of the current module file after being parsed.

Remember that __filename is not really a global variable.

2.4 module

Remember that module is not really a global variable.

2.5 require()

Remember that require() is not really a global variable.

2.6 exports

Remember that exports are not really global variables.

2.7 setImmediate and clearImmediate

2.8 setTimeout and clearTimeout

2.9 setInterval and clearInterval

2.10 console

For printing to standard output and standard error

2.11 process

The process object provides information about and control over the current Node.js process.

2.12 URL

URL Utilities for URL processing and parsing

2.13 events

The events module is Node's implementation of the "publish/subscribe" model. An object passes messages to another object through this module. This module provides a constructor through the EventEmitter attribute.

3. globalThis

3.1 What is globalThis?

JS language is increasingly being used in various environments. In addition to the most common browsers, it can also run on servers, smartphones, and even robotic hardware.

Each environment has its own object model and provides a different syntax for accessing global objects. For example, in a web browser, the global object can be accessed through window, self, or frames. However, in Node.js, these properties do not exist and globals must be used instead.

globalThis aims to consolidate the increasingly fragmented methods of accessing global objects by defining a standard global property. This proposal was included in the ES2020 standard. All popular browsers, including Chrome 71+, Firefox 65+, and Safari 12.1+, already support this feature. You can also use it in Node.js 12+.

Content extension:

NodeJS - global object

function global() {
    // Global variables. __filename represents the file name of the currently executing script.
    console.info('__filename: ' + __filename ); // __filename: D:\github\nodejs-test\requestHandlers.js

    // Global variables. __dirname indicates the directory where the currently executing script is located.
    console.info('__dirname : ' + __dirname ); // __dirname : D:\github\nodejs-test

    // Global function. setTimeout(cb, ms) The global function executes the specified function (cb) after the specified number of milliseconds (ms). setTimeout() executes the specified function only once. Returns a handle value representing the timer.
    setTimeout(function () {
        console.info('setTimeout: I only execute it once.');
    }, 2000);

    // Global function. The clearTimeout( t ) global function is used to stop a timer previously created with setTimeout(). The parameter t is the timer created by the setTimeout() function.
    let t = setTimeout(function () {
        console.info('clearTimeout: I can't execute it.');
    }, 2000);
    clearTimeout(t); // Clear timer // Global function. setInterval(cb, ms) global function executes the specified function (cb) after the specified number of milliseconds (ms).
    let tt = setInterval(function () {
        console.info('setInterval: I execute it every 2 seconds.');
    }, 2000);

    // Global function. The setInterval() method will call the function repeatedly until clearInterval() is called or the window is closed.
    setTimeout(function () {
        clearInterval(tt); // Clear timer }, 5000);

    // Global object. console
    console.info('console: I also belong to global.');

    // Global variables. Attributes of the global object. process
    console.info('process current directory: ' + process.cwd()); // Output current directoryconsole.info('process current version: ' + process.version); // Output current versionconsole.info('process platform information: ' + process.platform); // Output platform information}

Output:

__filename: D:\github\nodejs-test\requestHandlers.js
__dirname : D:\github\nodejs-test
console: I also belong to global.
process Current directory: D:\github\nodejs-test
process Current version: v10.15.3
process platform information: win32
setTimeout: I only execute it once.
setInterval: I execute it every 2 seconds.
setInterval: I execute it every 2 seconds.

This concludes this article on the differences between global objects in nodejs and browsers. For more information about global objects in nodejs, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Nodejs learning notes: Global Objects

<<:  Detailed explanation of the process of zabbix monitoring sqlserver

>>:  The process of installing MySQL 8.0.26 on CentOS7

Recommend

HTML table markup tutorial (2): table border attributes BORDER

By default, the border of the table is 0, and we ...

MySQL Database Indexes and Transactions

Table of contents 1. Index 1.1 Concept 1.2 Functi...

Detailed explanation of Nginx's rewrite module

The rewrite module is the ngx_http_rewrite_module...

Installation process of zabbix-agent on Kylin V10

1. Download the installation package Download add...

Don’t bother with JavaScript if you can do it with CSS

Preface Any application that can be written in Ja...

vue uses Ele.me UI to imitate the filtering function of teambition

Table of contents Problem Description The general...

Detailed explanation of the principle of Docker image layering

Base image The base image has two meanings: Does ...

A brief summary of my experience in writing HTML pages

It has been three or four months since I joined Wo...

Detailed explanation of Mysql communication protocol

1.Mysql connection method To understand the MySQL...

jQuery realizes the picture following effect

This article shares the specific code of jQuery t...

Vue-pdf implements online preview of PDF files

Preface In most projects, you will encounter onli...

JavaScript removes unnecessary properties of an object

Table of contents Example Method 1: delete Method...

Steps to package and release the Vue project

Table of contents 1. Transition from development ...