Global object
Global object in browser: Window
Global objects in Node:
- Global: All attributes can be accessed globally
- Process: There is only one process object and the process name can be set
- The process.nextTick function: Equivalent to the setImmediate function, mediate plans the function’s execution events into the next event loop
- The console:
console.log()
,console.error()
Module system
Core three global variables: require, Module, exports
Absolute module
Modules that can be found directly in node_modules can be imported directly without adding pathnames
The relative module
Through the path to find the relative module location, thereby introducing. Methods or attributes defined in the module can be exposed via export.method=function(){}. Or you could rewrite modules.exports completely.
The event
Event Emitter
The DOM API that the browser uses to listen for events
- addEventListener
- removeEventListener
- dispatchEvent
Node exposes the Event EmitterAPI
- .on
- .emit
- .removeListener
var EventEmitter=require('events').EventEmitter
var a=new EventEmitter()
a.on('event'.function(){
console.log('event called')
})
a.emit('event')
Copy the code
The original code in the book reads like this: A = New EventEmitter, finding that new an object without parentheses is indeed a knowledge blind spot. First we need to know what a default constructor is, what an overloaded constructor is, and what a compound default constructor is.
Default constructor: The compiler provides a default constructor for each class, called the default constructor. The default constructor argument is generally null.
Compound default constructor: A user-defined constructor that has default values for all formal parameters, called a compound default constructor.
(1) Parentheses
1. If the parentheses are empty, that is, there is no argument item, it is understood that the default constructor is called; 2. If the parentheses are not empty, there is an argument item, which can be interpreted as calling an overloaded constructor, or compound the default constructor.Copy the code
(2) Call the default constructor without parentheses, or compound the default constructor.
That is, the default function provided by the compiler is called without parentheses. When the default constructor is no longer available, the user must define a compound default constructor for the class (select one of the custom constructors and assign default values to all formal parameters).
Getting back to the point,eventListener can also be added to your own class to listen for events in that class
var EventEmitter=require('events').EventEmitter
, MyClass=function(){
this.speak=function(){
console.log('speak')
}
}
MyClass.prototype.__proto__=EventEmitter.prototype
,a=new MyClass
a.on('speak'.function(){
console.log('I heard speak')
})
a.emit('speak')
Copy the code
Note that on is listening for emit events instead of MyClass method speak
ServerRequest
ServerRequest is derived from EventEmitter and distributes event data and Ends, such that it caches request data and waits until all data is received.
var http=require('http')
http.Server(function(req,res){
var buf=' '
req.on('data'.function(data){
buf+=data
})
req.on('end'.function(){
console.log('transmit over')})})Copy the code
buffer
A buffer represents a fixed-allocation global object consisting of eight-bit byte elements. Help us create a string to represent a complex file (image like)
var buffer=new Buffer(string,'base64')
require('fs').writeFile(' ',buffer)
Copy the code