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

Embed codes for several older players

The players we see on the web pages are nothing m...

MySQL 8.0 can now handle JSON

Table of contents 1. Brief Overview 2. JSON basic...

What is HTML?

History of HTML development: HTML means Hypertext...

Detailed explanation of MySQL 8's new feature ROLE

What problems does MySQL ROLE solve? If you are a...

About Vue virtual dom problem

Table of contents 1. What is virtual dom? 2. Why ...

Steps to transfer files and folders between two Linux servers

Today I was dealing with the issue of migrating a...

Nginx configuration SSL and WSS steps introduction

Table of contents Preface 1. Nginx installation 1...

jQuery achieves large-screen scrolling playback effect

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

Let's talk about the difference between containers and images in Docker

What is a mirror? An image can be seen as a file ...

Detailed explanation of three ways to set borders in HTML

Three ways to set borders in HTML border-width: 1...

Summary of frequently used commands for Linux file operations

0. New operation: mkdir abc #Create a new folder ...

Solution to the horizontal scroll bar in iframe under IE6

The situation is as follows: (PS: The red box repr...

vue3 custom directive details

Table of contents 1. Registering custom instructi...

Solve the problem of Syn Flooding in MySQL database

Syn attack is the most common and most easily exp...