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 are the differences between nodejs and browser

2025-02-24 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 nodejs and browser". In daily operation, I believe many people have doubts about the difference between nodejs and browser. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between nodejs and browser?" Next, please follow the editor to study!

Differences: 1. In the global environment, this in node points to global, while this in browser points to window;2, DOM and BOM can be operated directly or indirectly in browser, but DOM and BOM;3 can not be operated in node, and browsers cannot perform file operations in nodejs.

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

I. 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 the node environment. Because these API are browser-level encapsulation, they are not stored in the javascript. Of course, node also provides a lot of node-specific API.

II. Js engine

Different browser vendors provide different browser kernels in browsers, and browsers rely on these kernels to interpret the js we write. But considering the small differences between different kernels, we need compatibility. Fortunately, there are some excellent libraries to help us deal with this problem, such as jquery, underscore, and so on.

Nodejs is based on Chrome's JavaScript runtime, that is, it actually encapsulates the GoogleV8 engine (for Google Chrome browsers). The V8 pilot engine executes Javascript very fast and the performance is very good.

NodeJS doesn't provide a simple wrapper and then an API call, and if that were the case, it wouldn't be as popular as it is now. Node optimizes some special use cases to provide an alternative API to make V8 run better in a non-browser environment. For example, in a server environment, dealing with binary data is usually necessary, but Javascript does not support it enough, so V8.Node adds the Buffer class to handle binary data conveniently and efficiently. Therefore, Node not only uses V8 simply, but also optimizes it to make it more powerful in all environments.

Js engines are fixed, but also corresponding to horse compatibility.

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.

IV. 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.

Of course, there are many differences between node and browsers, which I will add at any time.

At this point, the study on "what's the difference between nodejs and browser" 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