Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between nodejs and global objects in browsers

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the difference between global objects in nodejs and browsers". In daily operation, I believe many people have doubts about what is the difference between global objects in nodejs and browsers. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the difference between global objects in nodejs and browsers". Next, please follow the editor to study!

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

The top-level global object in the browser is the variable declared by window,var that is bound to the window object by default.

1. Definition of global object

Concept: objects that can be accessed anywhere in the program are called global objects. The properties of the object are called global variables.

2. Summary of global variables in NodeJS

Here we summarize the global variables that we often use in nodejs.

2.1 Buffer Class

Buffer, also known as a "buffer", its function is to open up an area in memory to store binary data.

2.2 _ _ dirname

_ _ dirname, which returns the absolute path of the folder (directory) where the current module file is parsed.

Remember, _ _ dirname is not really a global variable.

2.3 _ _ filename

_ _ filename, which returns the absolute path after the current module file has been parsed.

Remember, _ _ filename is not really a global variable.

2.4 module

Remember, module is not really a global variable.

2.5 require ()

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

2.6 exports

Remember, exports is not really a global variable.

2.7 setImmediate and clearImmediate

2.8setTimeout 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 controls the current Node.js process.

2.12 URL

URL is a utility for URL processing and parsing

2.13 events

The events module is Node's implementation of the publish / subscribe pattern (publish/subscribe). One object passes a message to another object through this module. This module provides a constructor through the EventEmitter attribute.

3. GlobalThis

3.1What is globalThis?

The JS language is increasingly used in a variety of 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 different syntax to access global objects. For example, in a Web browser, global objects can be accessed through window,self or frames. However, in Node.js, these properties do not exist, and you must use global.

GlobalThis aims to integrate increasingly decentralized methods of accessing global objects by defining a standard global property. The proposal is incorporated into the ES2020 standard. This feature is already supported by all popular browsers, including Chrome 71 +, Firefox 65 +, and Safari 12.1 desktop. You can also use it in Node.js 12 +.

Content extension:

NodeJS-global global object

Function global () {/ / global variable. _ _ filename represents the file name of the currently executing script. Console.info ('_ filename:'+ _ _ filename); / / _ _ filename: d:\ github\ nodejs-test\ requestHandlers.js / / global variable. _ _ dirname indicates the directory in which the script is currently executed. Console.info ('_ dirname:'+ _ _ dirname); / / _ _ dirname: d:\ github\ nodejs-test / / global function. The setTimeout (cb, ms) 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 that represents a 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 by setTimeout (). The parameter t is a 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. The 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 keeps calling the function 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 variable. Properties of the global object. Process console.info ('process current directory:' + process.cwd ()); / output current directory console.info ('process current version:' + process.version); / / output current version console.info ('process platform information:' + process.platform); / / output platform information}

Output:

_ _ filename: d:\ github\ nodejs-test\ requestHandlers.js__dirname: d:\ github\ nodejs-testconsole: I also belong to global. Process current directory: d:\ github\ nodejs-testprocess current version: v10.15.3process platform Information: win32setTimeout: I will only execute it once. SetInterval: I execute it every 2 seconds. SetInterval: I execute it every 2 seconds. At this point, the study on "what is the difference between nodejs and global objects in browsers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report