========== about asynchronous ===========
What is a callback?
Callbacks are the most basic method of asynchronous programming, such as Nodejs, the need to execute asynchronous logic in sequence, the general use of subsequent delivery, that is, the subsequent logic encapsulated in the callback function as the starting function of the parameters, layered nesting, use this way to let the program in the way we want to go through the process.
function Learn (something) {
Console.log (something)
}
function we (callback,something) {
something + = ' is cool '
Callback (something)
}
We (Learn, ' Nodejs ')
You can also use anonymous functions
We (function (something) {
Console.log (something)
}, ' Jady ')
What is synchronous, asynchronous?
Synchronization is the execution of a task, after which a task waits for the previous task to end and then executes, and the program executes in the same order as the task. For example, the browser-side JS is single-threaded, JS code can only be executed sequentially.
Async each task has one or more callback functions, the previous task executes after the end of the execution is not the next task, but the execution of the callback function, the latter task is not to wait for the end of the previous task to execute, the execution order of the program is inconsistent with the order of the task.
var c = 0
function PrintIt () {
Console.log (c)
}
Function Plus (callback) {
SetTimeout (function () {
c + = 1
Callback (c)
},1000)
}
Plus (PrintIt) asynchronous execution, will print after 1000ms 1
PrintIt () synchronous execution, will immediately print 0
What is IO?
Disk write and read out, the data in and out, in the Nodejs is actually for the file system, database and other resources to provide interfaces to the file system to send a request, do not wait for the hard disk, the hard disk is ready after the non-blocking interface will notify the node
For single-threaded nodejs, we can do asynchronous programming through callbacks to achieve a non-blocking effect, so when is the callback function called in Nodejs? That is related to event and event driven. In Nodejs, many objects can trigger events, such as reading a file, opening a file, a client connecting to the server, triggering an event, and all the objects that can trigger the event are events. An instance of Eventemitter. We register a callback function for each event, and this callback function is not executed immediately, only when the event occurs, the callback function is called, the function is called event-driven, the registration callback is an event-driven callback. If there are a lot of asynchronous operations, such as asynchronous operations of IO, or timer-controlled delay operations, they will call the response callback function when they are finished, thus accomplishing some intensive tasks without blocking the entire program execution process, so many events require an event loop. The event loop is a callback function queue, and when the asynchronous function executes, the callback function is pressed into the queue, and in Nodejs, a single thread queries the queue for events, and when read to an event, the JavaScript function associated with the event is invoked.
========== about asynchronous ===========
========== about scopes and context ==========
Scope and calling functions, about the ability to access variables
Scope is divided into local scope and global scope, in the local scope can access to the global scope of the variable, but in the local outside of the scope of the function to access the local action set variables
The context is related to the This keyword and is a reference to the current executable code that is called
This always points to the object that called this method
The this in JS is usually the owner of the current function.
This is a keyword for JS that represents an internal object that is automatically generated when the function is run, and can only be used inside the function
The function context executes the object according to the current operating environment, pointing to the global object in the global run environment, depending on how the function is tuned inside the function
The methods to be adjusted include:
1. Methods as objects
This is in var pet = {
Words: ' ... ',
Speak:function () {
Console.log (This.words)
Console.log (This===pet)
}
}
Inside the Pet.speak () method, this points to the object that called the method
Printing results are:
...
True
2. Invocation of functions
This points to the global object in the execution environment (browser->window nodejs->global)
function Pet2 (words) {
This.words = words
Console.log (This.words)
Console.log (This===global)
}
Pet2 (' ... ')
Printing results:
...
True
3. Constructors
This method is called by the instance object, which points to this instance object
function PEt3 (words) {
This.words = words
This.speak = function () {
Console.log (This.words)
Console.log (This)
}
}
var cat = new PEt3 (' Miao ')
Printing results:
Miao
{words: ' Miao ', Speak: [Function]}
The JS function has a defined context and runtime context
Change the context method (change the contents of this point to make inheritance easier)
Call (list)
Apply (Array)
var pet = {
Words: ' ... ',
Speak:function (say) {
Console.log (say+ "+this.words)
}
}
var dog = {
Words: ' Wang '
}
Pet.speak.call (dog, ' speak ')
Operation Result:
Speak Wang
function Pet (words) {
This.words = words
This.speak = function () {
Console.log (This.words)
}
}
function Dog (words) {
Pet.call (This,words)
}
var dog = new Dog (' Wang ')
Dog.speak ()
Operation Result:
Wang
========== about scopes and context ==========
node. js Getting Started Tutorial (iii): api-preparing knowledge