当前位置:网站首页>ES6 detailed explanation
ES6 detailed explanation
2022-07-25 10:11:00 【Zero degrees Celsius】
Es6 Detailed explanation
One 、 summary
- ECMAScript It's a kind of Ecma The international community has adopted ECMA-262 Standardized scripting language , It is widely used on the world wide web , It's often called JavaScript, It can be understood as JavaScript A standard of .
- rapid growth :es2.0 To es6.0
Two 、Es6 The grammar of
1. let and const command
<script> // The traditional way of defining variables and constants Unified use var //ES6 How to define : // Defining variables let name1 = "xqh"; // Define constants const PI2 = Math.PI; console.log(name1) console.log(PI2); </script>
- let/const/var The difference between
- let and const solve :
- var It will lead to the problem of variable penetration
- var It will lead to the problem of constant modification
<script> for(var i= 0;i<5;i++){
console.log(i); } console.log(i); // The result is traversal 0 1 2 3 4 5, Cause variable penetration Live reload enabled. // Change to let, Solve the problem of variable penetration 0 1 2 3 4 const PI = Math.PI; PI=100; console.log(PI); // use var Will print out 100, Violation of constant immutability . Change to const, Is immutable // In actual development and production , If it's a small program ,uniapp Or some scaffolding , You can use it boldly let and const // But if it is web In development , I suggest you still use var, Because it is not supported in some lower versions of browsers let and const </script>
2. Template string
<script> // The string will involve the dynamic part var person={
name:"xqh", address:"jiangxi", link:"http://www.baidu.com" } // Traditional stitching , use + And quotation marks let address = " I am a "+person.name+", I live in "+person.address+", Website is "+person.link; console.log(address); // use es6 Syntax splicing let address1 = ` I am a ${
person.name}, I live in ${
person.address}, Website is ${
person.link}`; console.log(address1); </script>
3. The default parameters of the function are the same as the arrow function
- Function default parameters
<script> // Function default parameters function sum(a=200,b=100){
// Default parameters return a+b; } var result = sum(200);// Only one is passed to a,b If there is a default parameter, use the default parameter console.log(result);//300 </script>
- Arrow function
<script> // Arrow function - a key ( In future project development : Like an applet ,uniapp, Some scaffolds are widely used ) var sum = function(a,b){
return a+b; }; // improvement Get rid of function, Add an arrow after the bracket , If the logic code has only return You can directly omit , If there is only one parameter, brackets can also be omitted var sum = (a,b)=>a+b; var result = sum(2,3); console.log(result); // Example var arr = [1,2,3,4,5,6]; var newarr = arr.map(function(obj){
return obj*2; }); console.log(newarr) // simplify var newarr1 = arr.map(obj=> obj*2); </script>
4. Object initialization shorthand
- If in an object key and value The same name can be defined as a
<script> let info={
title:" Jiangxi Agricultural University ", link:"www.baidu.com", go:function(){
console.log(" I ride a little blue car to class "); } }; //es6 Abbreviation // Because the object is key:value There is //1: If key Consistent with the name of the variable , You can define it only once //2: If value It's a function , You can put `:function` Get rid of , only () that will do var title=" Jiangxi Agricultural University "; var link = "www.baidu.com"; let info2 = {
title, link, go(){
console.log(" I ride a little blue car to class "); } }; console.log(info2); console.log(info2.link); console.log(info2.title); console.log(info2.go); </script>
5. Object to deconstruct
- Object to deconstruct —es6 Provide some quick ways to get object properties and behavior
<script> // The object is to key:value There is , There are two ways to get object properties and methods //1. adopt . 2 adopt [] var title=" Jiangxi Agricultural University "; var link = "www.baidu.com"; let info2 = {
title, link, go(){
console.log(" I ride a little blue car to class "); } }; // adopt . The way console.log(info2); console.log(info2.link); console.log(info2.title); info2.go; // adopt [] The way console.log(info2["title"]); console.log(info2["link"]); info2["go"](); //es6 Object structure -- In fact, it is a form of quickly obtaining properties and methods var {
title,link} = info2; // To restore it is var title = info2.title var link = info2.link </script>
6. Object propagation operators
- Object propagation operators
<script> // Object propagation operators var person={
name:" Study together ", address:" guangdong ", link:"www.kuangshen.com", phone:123456, go(){
console.log(" Start work "); } }; // Deconstruct var {
name,address,...person2}=person;//name/address Deconstructed . The rest will be person2 in , This is the propagation operator console.log(person2);// Print out person2 There is only link、phone、go(), because name、address Has been deconstructed console.log(name); console.log(address); </script>
- Simple application cases
<script> //java--- backstage // data format :var userPage={pages:10,users:[{},{}],pageNo:1,pageSize:100,totle:100}; // Asynchronous requests $.post("/user/search",function(res){
//res={pages:10,users:[{},{}],pageNo:1,pageSize:100,totle:100}; var userPage = {
pages:10,users:[{
},{
}],pageNo:1,pageSize:100,totle:100}; var {
users,...userPage2}=userPage;// So we can take pages,pageNo,pageSize,totle Take it all out , and users Take it out alone </script>
7. Array map and reduce Methods use
- Array map
<script> // Right arr Array each element *2 var arr =[1,2,3,4,5,6,7]; // The traditional way let newarr=[]; for(let i =0;i<arr.length;i++){
newarr.push(arr[i]*2); } console.log(newarr); //map It's easy to implement .map: Self contained loop , And the processed value will be backfilled to the corresponding position var newarr2 = arr.map(function(ele){
return ele*2 }) // simplify :var newarr2 = arr.map(ele=>ele*2); console.log(newarr2); //map Processing object data var users=[{
age:10,name:" Primary school "},{
age:12,name:" Xiao Hu "},{
age:15,name:" Little fat "}]; // Now we need to increase everyone's age var newusers=users.map(ele=>{
ele.age = ele.age+1; return ele; }); console.log(newusers); </script>
- reduce() Use
<script> //reduce Quickly sum the elements in the array var arr=[1,2,3,4,5,6,7,8,9,10]; var result=arr.reduce(function(a,b){
return a+b; }); // simplify var resule=arr.reduce((a,b)=>a+b); console.log(result); </script>
3、 ... and 、 Summary
- es6 Grammar can be applied to nodejs in , No mistake.
- Output with terminal
边栏推荐
- ThreadLocal&Fork/Join
- nodejs链接mysql报错:ER_NOT_SUPPORTED_AUTH_MODEError: ER_NOT_SUPPORTED_AUTH_MODE
- Eco introduction
- Exciting method and voltage of vibrating wire sensor by hand-held vibrating wire acquisition instrument
- I2C也可总线取电!
- FPGA basic advanced
- 手持振弦采集仪对振弦传感器激励方法和激励电压
- Introduction to armv8 general timer
- JS uses requestanimationframe to detect the FPS frame rate of the current animation in real time
- 基础背包问题
猜你喜欢

Probabilistic robot learning notes Chapter 2

安装 oh my zsh

Fundamentals of C language

CCF 201512-4 delivery

Terminal definition and wiring of bsp3 power monitor (power monitor)

Mlx90640 infrared thermal imager temperature measurement module development notes (I)

T5 paper summary

nodejs版本升级或切换的常用方式

Mlx90640 infrared thermal imaging sensor temperature measurement module development notes (II)

线程池的设计和原理
随机推荐
How Android uses ADB command to view application local database
CCF 201604-2 Tetris
Wechat applet jumps to other applets
ThreadLocal&Fork/Join
CCF 201503-4 network delay
FPGA basic advanced
About student management system (registration, login, student side)
MVC three-tier architecture understanding
@5-1 CCF 2019-12-1 reporting
字符串切片的用法
Common methods of JS digital thousand bit segmentation
ADC introduction
App lifecycle and appledelegate, scenedelegate
用Arduino写个ESP32看门狗
RedisUtil
字典树的使用
[recommended collection] with these learning methods, I joined the world's top 500 - the "fantastic skills and extravagance" in the Internet age
Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification
dp-851
[RNN] analyze the RNN from rnn- (simple|lstm) to sequence generation, and then to seq2seq framework (encoder decoder, or seq2seq)