Node. js 4 JS essentials that developers must know
- 2020-12-26 05:34:43
- OfStack
This article summarizes four Node.js points for developers.
1. Non-blocking (ES5en-ES6en) or asynchronous I/O
Because Node.es12EN1 is a server-side framework, one of its main tasks is to handle browser requests. In the traditional I/O system, each request is issued after the previous one has arrived. So this is called blocking (blocking)I/O. The server blocks other requests to process the current request, causing the browser to wait.
Node.js does not handle I/O processing in this manner. If a request takes a long time to process, Node.js sends the request to an event loop (event loop) and then continues to process the next 1 request on the call stack (call stack). When the delayed request is processed, it notifies Node.js and the browser responds with feedback.
Here is an example to illustrate.
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 the restaurant example, the waiter receives the menu order, waits for the meal to be served, and then brings the meal to the table when it is finished. He will refuse menu orders from other guests while the waiter waits for the meal to be served.
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 non-blocking mode, the waiter tells the chef the menu order he has received and then goes to receive the next table order. When the first table is finished, he serves the meal to the other guests and then goes on to take orders from the other guests. This will prevent the attendant from wasting time by blocking instructions.
2. The prototype (Prototype)
Prototypes are a complex concept in JS. In a typical inheritance mechanism language such as Java or C++, to achieve code reuse, you must first create a class and then generate objects through it or through class extensions. But there is no similar class concept in JS. After creating an object in JS, you need to extend the object or create a new object through it. This is called prototype inheritance (prototypal inheritence).
Each JS object is connected to a prototype object and inherits the properties of that object. Each object is associated with Object.prototype of the predefined JS. If you use ES66en. propName or obj[' propName'] > If the search fails, you can try to find the property by obj.hasOwnProperty (' propName'), which will look for the property in the prototype object when it runs. If the attribute does not exist in the stereotype chain, the undefined value is returned.
Let's illustrate with the following examples:
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 a prototype based object. Here, we add an create method to the object function. The create method creates an object modeled on another object and passes it in as a parameter.
When we change a new object, its prototype remains the same. However, when we change a prototype object, the change affects all objects based on that prototype.
3. The component (Modules)
If you have used packages in Java, the components of ES91en.js are similar. If not, don't worry; Components are simple JS files that implement a specific function. The point of the component pattern is to make your job easier. To use components, you must import the JS file as you did the package 1 import in JAVA. There are two types of components in ES96en.js
Core components (Core Modules) - The core components are precompiled in conjunction with the ES102en.js library. The goal is to open up the functions that programmers often use to avoid duplication of effort. Common core components are HTTP, URL, EVENTS, FILE SYSTEM and so on.
User-defined components (UserDefined Modules) - User-defined components are components that are provided to the user to perform specific functions. Custom components can come in handy when the core components are insufficient for the programmer's needs.
Components are extracted via the require function. If this is a core component, then the parameter is the component name. If this is a user-defined component, then the parameter is its component path in the file system. Such as:
// extract a core module like this
var http = require('http);
// extract a user defined module like this
var something = require('./folder1/folder2/folder3/something.js');
4. Callback (Callbacks)
In JS, functions are class 1 objects. This means that you can do all the things you would do with a normal object. Such as assigning functions to a variable, passing these as arguments to methods, declaring them as properties of objects, and even returning them from functions.
Callbacks are asynchronous functions in JS and can be passed as arguments to other functions or executed or returned from other functions before being executed. This is the basic concept of a callback.
When we pass a callback function as an argument to another function, we pass only the definition of the function; In other words, we don't know the execution time of the callback function. This is entirely dependent on the callback function mechanism. It will be called back at a later point in time. This is the basic concept of non-blocking or asynchronous behavior of ES128en.js, illustrated by the following example:
setTimeout(function() {
console.log("world");
}, 2000)
console.log("hello");
This is one of the simplest calls. We pass an anonymous function as an argument to register the console output record for the setTimeout function. Since this is just a function definition, we don't know when the function will be executed. This depends on the second parameter of the setTimeout function, after 2S.
First, the second recording statement records the output to the console, and after 2S, the recording statement in the callback function records the output.
// output
hello
world
Write in the last
Node.js developers should thoroughly understand and master the above four points, and I suggest you do more to understand the meaning of these four points.