Four js highlights required by Node. JS developers _ node. js-js tutorial

Source: Internet
Author: User
This article mainly introduces four js key points that Node. JS developers must understand. Node. js is a server-oriented framework based on Chrome's powerful V8JS engine. Although it is compiled by C ++, it and its applications run on JS. If you need it, you can refer to this article to summarize four Node. js points for developers.

1. Non-blocking or asynchronous I/O

Because Node. js is a server-side framework, one of its main tasks is to process browser requests. In traditional I/O systems, each request is sent only after the previous request arrives. So this is called blocking I/O. The server blocks other requests to process the current request, causing the browser to wait.

Node. js does not perform I/O processing in this way. If a request needs to be processed for a long time, Node. js sends the request to an event loop and continues to process the next request in the call stack. When the processing of the delayed request is completed, it notifies Node. js and the browser responds to the feedback.

Here is an example.

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 receives the menu command, waits for the food to be processed, and then ends the food to the table after the food is processed. While waiting for food processing, the waiter rejects the menu instructions from other guests.

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 will inform the cook of the menu command he received and then receive the command from the next table. When the first table of meals is processed, he will serve the guests and continue to receive instructions from other guests. In this way, the waiter will not waste time due to blocking commands.

2. Prototype)

Prototype is a complex concept in JS. In typical Inheritance Mechanism languages such as Java or C ++, to achieve code reuse, you must first create a class and then generate an object through it or generate an object through class extension. But there is no similar class concept in JS. After creating an object in JS, you need to use it to expand the object or create a new object. This is called prototypal inheritence ).

Each JS object is connected to a prototype object and inherits the attributes of the object. Each Object is associated with the Object. prototype of the predefined JS. If you use obj. you can use the propName or obj ['propname'> method to find the object attributes but fail to search for them. hasOwnProperty ('propname') is used to search for attributes in the prototype object. If the property does not exist in the prototype chain, the undefined value is returned.

Let's use the following example to illustrate:

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 a create method for the object function. The create method creates an object based on another object and passes it as a parameter.

When we change a new object, its prototype remains unchanged. However, when we modify the prototype object, the change affects all objects based on the prototype.

3. Modules)

If you have used packages in Java, the components of Node. js are similar. If not, do not worry; components are actually simple JS files for specific functions. The component mode makes your work easier. To use components, you must import JS files like importing packages in JAVA. Node. js has two components

Core Modules )-The Core Component is pre-compiled in combination with the Node. js library. The purpose is to open up the features frequently used by programmers to avoid repeated efforts. Common core components include HTTP, URL, EVENTS, and file system.

User-Defined components (UserDefined Modules )-Custom components are provided for users to implement specific functions. Custom components can be used when the core components are insufficient to meet the needs of programmers.

Components are extracted using the require function. If this is a core component, the parameter is the component name. If this is a custom component, the parameter is the component path 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 (Callbacks)

In JS, a function is the first type of object. That is to say, you can perform all operations on functions as with regular objects. For example, assign a function to a variable, pass these parameters to the method, declare them as object attributes, or even return them from the function.

Callback is an asynchronous function in JS and can be passed as a parameter to another function or executed or returned from another function before execution. This is the basic concept of callback.

When we pass a callback function as a parameter to another function, we only pass the definition of the function. In other words, we do not know the execution time of the callback function. This relies entirely on the callback function mechanism. It will call back later. This is the basic concept of Node. js's non-blocking or asynchronous behavior. It can be described in the following example:

setTimeout(function() {  console.log("world");}, 2000)console.log("hello");

This is the simplest call. An anonymous function is passed as a parameter to register the output records of the console for the setTimeout function. Because this is just a function definition, we do not know when the function will be executed. This depends on the second parameter of the setTimeout function, that is, 2 seconds later.

First, the second record statement records the output to the console. After 2 seconds, the record statement in the callback function records the output content.

// outputhelloworld

Conclusion

The above four points should be thoroughly understood and mastered by Node. js developers. We recommend that you learn more about the meanings of these four points.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.