First, ECMASCRIPT6 (es2015) ES6 syntax
ES6/ES2015, on the basis of ES5 to expand a lot of new features, we want to learn is only part of the ES6 in the common new features, these features in use must be cautious, because some of them JS code in some browsers is not compatible, But all the code written on the server side basically supports the ES6 notation.
1.0 To turn on strict mode:
If you need to open in the function, add the string "use strict" in the first line of the function, if you need to open in the JS code, in the first line of JS add the short character "use strict".
This is a feature of ES5, the purpose of setting up "strict mode", mainly has the following several:
1) eliminate some unreasonable and unreasonable JavaScript grammar, reduce some strange behavior;
2) To eliminate some unsafe code operation, to ensure the security of code operation;
3) Improve the efficiency of the compiler, increase the speed of operation;
4) Pave the future for a new version of JavaScript.
Attention:
1) Direct undeclared variables can be used in normal mode, but not in strict mode
2) Normal mode can pass two parameters with the same name in a function, but not in strict mode
1.1 Define variables: let (similar to Var)
There is always a bug in JavaScript that takes value at Var
1) variables declared by Var will increase with the amount of change
2) var does not have block level scope
3) Var can repeatedly define a variable
While these statements are absolutely not allowed in the background development language, in order to standardize the declaration of variables ES6 introduced a new keyword let instead of VAR declaration variable, let features:
1) The variable that let declares does not have the variable promotion
2) let exists block-level scope
3) Let cannot define variables repeatedly
Note: Use the Let keyword to add "using strict".
1.2 Defining Variable const
function: Once a constant is declared:
1) value can no longer be changed
2) constants have block-level scopes
3) do not declare constants in blocks
4) No variable promotion, first declared after use
5) You cannot declare a constant with the same name
6) must be assigned the initial value, or error
7) If the object is declared, the address of the object cannot be changed, but its internal properties can be changed.
1.3 extension of the string
If you want to determine if there is "world" in the string "Hello World", the method in the string is only indexof, and in ES6 we provide other methods:
Includes (): Returns a Boolean value indicating that a parameter string was found
StartsWith (Str[,num]): Returns a Boolean value indicating whether a parameter string was found at the beginning of the original string
EndsWith (Str[,num]): Returns a Boolean value indicating whether the argument string is at the end of the original string
Repeat (num): Repeats the called string by num times
Note: num can be a positive integer, positive decimals (decimals rounded), or even a string type number, or a negative number or a non-numeric string error.
1.4 template syntax: ' Template string '
Structure: ' Template content ${code} ';
Note: Code can be a variable, can be a method name, or it can be a JS expression.
1.5 arrow Function: () =>{}
JS in the location of the use of anonymous functions too many, fixed structure for function () {}, in order to facilitate writing, es6 a new way to simplify the anonymous function: remove function to = =, structure is () =>{}
Evolution Process:
Procedure one: Remove the function keyword
Arr.each (function (m,n) {
Conosle.log (n);
});
Get:
Arr.each ((m,n) =>{console.log (n);});
Procedure two: If only one line of code can remove the curly brace and return keyword
Arr.each ((m,n) =>console.log (n));
Procedure three: If the parameter has only one, you can remove the parentheses in the argument
Arr.each (N=console.log (n));
Summary:
parameter features:
1) Only one parameter can be removed ()
2) Multiple parameters, can not be removed
3) parameters not, can not be removed
Method body Features:
1) method body only one sentence, you can omit {}
2) method body only one sentence, if there is return, you can also omit the return
3) If you have multiple lines of code, you cannot omit
The arrow function does not have a fixed notation, it is based on the parameters of the current method and the return value of the method.
Use note points:
1 the This in the arrow function points to the problem:
The arrow function does not have its own this, which is written inside the function body, and will follow the scope to find the nearest real present.
2 The this inside the arrow function is the object that the definition is in, not the object that is used, and does not change
3 The arrow functions cannot function as constructors:
4 There is no arguments in the arrow function, and the arguments in the arrow function points to the outer arguments
The file System (FS) is the core module of document reading and writing .
2.1 Buffer object:
Role: All data in the computer world is in the form of binary, whether we see images, text or browser to the server and request, and the server sent to the browser response. So in order to be able to accept and store this data, Nodejs must have the ability to manipulate binary data, and the buffer object is used to store the binary data.
using the Buffer object
1) Create:
var buffer = new buffer (5);
2) Clear:
Buffer.fill (0);
3) Write content
Buffer.write (' I am abc ');
Note: In buffer, a Chinese character occupies three bytes and one byte in English. Because the length of the array is fixed, future data loss may result if too much content is stored in the object
4) Convert an array to a string
Buffer.tostring ();
5) The length of the array on demand
Let buf = new Buffer (' Hello World ');
Console.log (buf.tostring (););
Buffer Common operations:
-new Buffer (parameter): Create a Buffer object
-buf.write (): Write content
-buf.length: Gets the length of the data in the buffer object
-buf.slice: Used to get the specified length of data at the specified location
-buf.tostring (encoding): Converts binary data to a specified encoded string, default is Utf-8
-buffer.isencoding (encoding): Determines whether the current encoding format is supported in Nodejs.
Buffer supported encodings:ascil,utf8node default encoding, Utf16le,ucs2,base64,hex,binary
If you encounter a file gb2312 that you want to nodejs support, you need to use a third-party package Iconv-lite
Summary: The study of buffer learn about three points:
1) Understand that the buffer object is being manipulated in the read and write operation of the file in the future
2) understand the encoding format supported by buffer only
3) If you encounter an unsupported situation, you can find a third-party package.
2.2 Operating Directory
The operations directory is generally divided into two situations: one is synchronous, and the other is an asynchronous operation.
What is synchronization:
When you do an event,
What is async:
When you do an event, you can do other things after you finish the first thing, and then deal with the follow-up later.
Summary: The efficiency of asynchrony is much higher.
A. To Create a folder: mkdir
Step: 1) referencing the core module
Const FS = require ("FS");
2) Create a folder
Fs.mkdirsync ("./abc");//Do not recommend this method, the synchronization efficiency is too low
Fs.mkdir ("./abc/a", function (Err) {
Judge
})
Note: If there is no ABC, there is no way to create a
B. To Delete a folder: rmdir
Consistent with the above.
Note: RmDir can only remove empty folders.
C. Determine if a directory exists
Fs.exists (path, (exists) =>{})
2.3 Manipulating Files
A. Write file ( increase )WriteFile AppendFile
Fs.writefile (File,data,[,opionts], (ERR) =>{})//Overwrite file contents
Attention:
1) This method if the file on the path does not exist will create the file and then write to the file, if the directory does not exist is not created
2) Each write overwrites the contents of the current file
Fs.appendfile (Path,data, (err) =>{});//Append file
B. Read the file ( check )ReadFile
Fs.readfile (Path, (err,content) =>{})
c. file Rename (change)rename
Fs.rename (Path,path, (err) =>{});
d. Delete file ( deleted )unlink
Fs.unlink (Path, (err) =>{})
F. monitoring file watchfile
Fs.watchfile (Filename,{persistent:true,intrval:50}, (Curr,prev) {})
Curr: Represents the version of the most recent file
Prev: Represents the previous revision
Callback: The callback function is executed when the file is modified.
Note: If you want to read a file and do something else, you must not put the code outside the callback function, or you will not finish the operation.
Nodejs's fourth day study