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 node environment and browser environment

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

Share

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

Editor to share with you what is the difference between the node environment and the browser environment. I hope you will get something after reading this article. Let's discuss it together.

Differences: 1. This in node points to global, while browsers point to window;2 and Node using CommonJS standards, while browsers use ES Modules standards. 3. Js in browsers can operate DOM, but not in node; 4. Imacuro read and write operations are different; 5. Module loading is different.

This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.

The difference between node environment and browser environment

1. The direction of this in the global environment

This points to global in node and this to window in browsers, which is why a root is defined in underscore.

Var root = typeof self = 'object' & & self.self = self & & self | | typeof global = =' object' & & global.global = global & & global | | this

And there are a lot of API encapsulated under the window in the browser, such as alert, document, location, history and so on. We can't xxx (); or window.xxx (); in a node environment. Because these API are browser-level encapsulation, they are not available in pure javascript. Of course, node also provides a lot of node-specific API.

2. Module standard

Node.js uses the CommonJS module system, and in the browser we begin to see the ES Modules standard being implemented.

In practice, this means that you temporarily use require () in Node.js and import in browsers.

3. DOM operation

In most cases, js in browsers operates DOM directly or indirectly (some virtual DOM libraries and frameworks). Because the code in the browser mainly works in the presentation layer. But node is a server-side technology. There is no foreground page, so we will not operate DOM in node.

4. Icano read and write

Unlike browsers, we need to read and write files like other server-side technologies, and nodejs provides more convenient components. It's a lot of trouble for browsers (to ensure compatibility) to open a local image directly in the page (don't tell me it's not easy, relative path. Try to know that either find a library or binary stream, or upload it with a network address displayed. Otherwise, why would someone want to build a js library), and all this node is done with one component.

5. Module loading

A feature of javascript is that the native API that does not provide package references executes all the things to be loaded at once, so it depends on the power of your closures. The things used are all together, there is no divide and rule, and it is particularly illogical and reusable. If the page is simple or the site is, of course, we can do it through some AMD and CMD js libraries (such as requireJS and seaJS). In fact, many large websites do this.

The API loaded by CMD's module is provided in nodeJS. If you have used seaJS, you should get started quickly.

Node also provides npm as a package management tool, which can manage the libraries we drink more effectively and conveniently.

Of course, ES6 on the browser side also has this supplement, and I believe it will be better in the future.

After reading this article, I believe you have a certain understanding of "what is the difference between node environment and browser environment". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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