In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "detailed explanation of the four basic concepts of Node.js". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "detailed explanation of the four basic concepts of Node.js".
1. Non-blocking or asynchronous Istroke O
Because Node.js is a server-side framework, one of its main tasks is to handle browser requests. In the traditional IWeiO system, the current request is issued only when the response to the previous request (the HTML page) has arrived. This is why it is called blocking IPUBO. The server blocks other requests in order to process the current request, which causes the browser to wait.
Node.js does not follow the principle of iUniver. If a request takes a long time, Node.js sends the request to the event loop (event loop) and continues to process the next request in the call stack (call stack). Once the pending request is processed, it tells Node.js and renders the response on the browser.
Use a virtual example to understand this:
Blocking I / O
/ / take order for table 1 and wait...var order1 = orderBlocking (['Coke',' Iced Tea']); / / once order is ready, take order back to table.serveOrder (order1); / / once order is delivered, move on to another table.// take order for table 2 and wait...var order2 = orderBlocking (['Coke',' Water']); / / once order is ready, take order back to table.serveOrder (order2) / / once order is delivered, move on to another table.// take order for table 3 and wait...var order3 = orderBlocking (['Iced Tea',' Water']); / / once order is ready, take order back to table.serveOrder (order3); / / once order is delivered, move on to another table.
In this restaurant example, the waiter gives the menu, waits for the order to be completed, and then returns to the table to serve according to the menu. When the current customer orders, the waiter waits and does not accept other customers' menus.
Non-blocking I / O
/ / take order for table 1 and move on...orderNonBlocking (['Coke',' Iced Tea'], function (drinks) {return serveOrder (drinks);}); / / take order for table 2 and move on...orderNonBlocking (['Beer',' Whiskey'], function (drinks) {return serveOrder (drinks);}); / / take order for table 3 and move on...orderNonBlocking (['Hamburger',' Pizza'], function (food) {return serveOrder (food);})
In this example, the waiter gets the menu, informs the cook, and then returns to get another menu. In the process of completing the first menu, he not only serves the current customer in order, but also accepts orders from other customers. Waiters don't waste time blocking orders from other customers.
two。 Prototype
Prototype is a complex concept of JavaScript. But because you use prototypes many times in Node.js, every JavaScript developer must understand this concept.
In languages that implement classical inheritance, such as Java, or C + +, for languages for code reuse, you must first write a class, and then create an object from it or extend it. However, the concept of a class does not exist in JavaScript. First create an object in JavaScript, then add your own object from that object, or create a new object. This is the so-called prototype inheritance and realization through the prototype.
Each JavaScript object is linked to a prototype object from which it can inherit properties. Prototypes are similar to classes in other OO languages, except that they are objects themselves. Each object is linked to an Object.prototype, and Object.prototype comes with a JavaScript predefinition.
If you look for properties through obj.propName or obj ['propName'] and the object does not have properties that can be checked by obj.hasOwnProperty (' propName'), then the JavaScript runtime looks for properties in its prototype object. If the prototype object does not have such properties, then check its prototype in turn until a match is found, or until it reaches the Object.prototype. If the property does not have a prototype chain, it results in an undefined value.
Understand this concept through the following sample code:
If (typeof Object.create! = = 'function') {Object.create = function (o) {var F = function () {}; F.prototype = o; return new F ();}; var otherPerson = Object.create (person)
When you create a new object, you must select an object that should be its prototype. Here, we add a method to Object function. This method creates a new object that uses another object as its prototype, and the prototype is passed to it as a parameter.
When we change a new object, its prototype is not affected. However, when we make changes to prototype objects, these changes are visible on all objects based on that prototype.
Prototype is a complex concept. I will explain it in detail in another article.
3. Module
If you have ever been exposed to packages in Java, the modules in Node.js are no different. If not, then don't worry. A module is a simple JavaScript file containing code for a specific purpose. Module mode is used to make your code easy to navigate and use. To use the module property, you need to require it in the JavaScript file, much like importing the package in the Java class.
There are two types of modules in node.js.
Core modules-these modules are precompiled using the Node.js library. The purpose of the core module is to provide code segments that developers often occur and repeat, which, if not available, can result in developers having to write the same code over and over again. Some common core modules are HTTP,URL,EVENTS,FILE SYSTEM, and so on.
User-defined modules-user-defined modules are modules that developers create within an application for a specific purpose. When the core module can not meet the desired function, the user-defined module is needed.
The module is extracted by require function. If it is a core module, then the parameter is just the name of the module. If it is a user-defined module, the parameter is the path of the module in the file system. For example:
/ / extract a core module like thisvar http = require ('http); / / extract a user defined module like thisvar something = require ('. / folder1/folder2/folder3/something.js'); 4. Callback function
In JavaScript, functions are considered to be the first kind of objects. This means that you can do all the things you can do with regular objects with these functions. You can assign a function to a variable, pass a function as an argument to a method, declare a function as an object property, or even return a function from a function.
A callback function is an anonymous function in JavaScript that can be passed as an argument to other functions, either executed or returned from the function for later execution. This is the basis of callback functions, the most widely used functional programming paradigm.
When we pass a callback function as an argument to another function, we can only pass the function definition. In other words, we don't know when this callback function will be executed. It all depends on the mechanism by which the function is called. It will be "callback" at some point in the future, hence its name. This is also the only basis for non-blocking or Node.js asynchronous behavior, as shown in the following example.
SetTimeout (function () {console.log ("world");}, 2000) console.log ("hello")
This is one of the simplest examples of callback functions. We pass an anonymous function as an argument that only needs to record some output to the setTimeout function on the console. It is the only function definition, but does not know when to execute. This needs to be decided after 2 seconds by calling the setTimeout function with the second parameter.
First, the second log statement records the output to the console, and then, two seconds later, the log statement in the callback function records the output.
/ / outputhelloworld at this point, I believe you have a deeper understanding of the "detailed understanding of the four basic concepts of Node.js". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.