ES6 of JS learning

1. Introduction to ES

ES6 is a general term referring to the version after EDMA JavaScript. It is the language standard for JS.

Nodejs

Introduction: It is a tool that focuses on the server, so that the server code can also be written using JS.

Install:

When installing Nodejs, it will come with an npm command, which is Node's package management tool.

Still need to use the cnpm tool? Be a little skeptical.

Babel

It is a transcoder that can convert ES6 code to ES5 code, so that it can be executed in older browsers. It means that you can use ES6 to write programs without worrying about whether the environment supports it. For example: The following is transcoding using ES6 code.

Install Babel tools:

Step 1: Use the command to install, cnpm install --save -dev @babel/core // (Note that this command is used under the project where we need to use transcoding tools) After installation, two files will appear: node_modules and { } package_json.

Step 2: Configure babel. Create the configuration file .balerc file. Type inside: { "presets": { }, "plugins": { } }

let command:

ES6 added the let command to declare variables. Its usage is similar to var, but the declared variable is only valid in the code block where the let command is located.

Difference: Take loops as an example.

 Note the following usage:

Const command

Declare a read-only constant that cannot be changed. (As long as it is declared, it must be initialized)

It is block-scoped; there is no constant promotion for const constants.

Object destructuring assignment:

It is to assign values ​​to multiple variables. For example:

let {name,age} = {name:"iwen" ,age:20 }

String expansion:

loop for( of ): for example, var str = "hello"

                           for(let i of str){ console.log(i) }

String template:

The format of this template: ${ }   

String method:

indexof() Determines whether a string is contained in another string. The following are the new methods added by ES6.

repeat():

Returns a new string, which means repeating the original string n times. The parameters are numbers.

pathStart(),pathEnd()

String completion length function, such as: the name of the character created in the game, you can use this method to complete the name.

Syntax: "dada".padStart(6,"li") //Get "lidada". The length is 6.

          "da".padStart(5,"li") //Get the length of "lilda" is 5.

act()

Accepts an integer as a parameter, returns the character at the position specified by the parameter (it is a single character, not a string), and supports negative indexes (that is, the position of the reciprocal).

If index is out of range, return undefined.

Array expansion:

Spread operator: 

The spread operator (spread) of the array is three points: ... //Convert an array into a sequence of parameters separated by commas.

Format: var arr=[1,2,3]; console.log(...arr); print 1, 2, 3

In JS, Math.max(~). A series of parameters are passed in, which cannot be used to judge the size of the array.

In Java, Math.max(~,~). It is to judge the size of the two parameters. The same cannot be used to determine the size of the array.

However, in js, you can use the array extension operator to convert the array into a series of data, and then get the maximum value of the array.

For example: Math.max(...arr). You can get the maximum value of the arr array.

It can also be used to combine arrays:

Guess you like

Origin blog.csdn.net/qq_55928086/article/details/131982326