当前位置:网站首页>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
边栏推荐
- CCF 201503-4 network delay
- Mlx90640 infrared thermal imager temperature measurement module development instructions
- CentOs安装redis
- vant问题记录
- CCF 201509-3 template generation system
- CCF 201604-2 Tetris
- The economic basis of a hardware siege lion
- Terminal definition and wiring of bsp3 power monitor (power monitor)
- 概率论与数理统计 3 Discrete Random Variables and Probability Distributions(离散随机变量与概率分布) (下篇)
- 入住阿里云MQTT物联网平台
猜你喜欢

nodejs链接mysql报错:ER_NOT_SUPPORTED_AUTH_MODEError: ER_NOT_SUPPORTED_AUTH_MODE

Swift creates weather app

Linked list -- basic operation

CentOs安装redis

Redux使用和剖析

Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification

canal实现mysql数据同步

ESP32连接阿里云MQTT物联网平台

¥ 1-2 example 2.2 put the union of two sets into the linear table

工程仪器振弦传感器无线采集仪的采集数据发送方式及在线监测系统
随机推荐
Temperature, humidity and light intensity acquisition based on smart cloud platform
Subtotal of rospy odometry sinkhole
Debug篇快捷键入门
pnpm简述
¥ 1-2 example 2.2 put the union of two sets into the linear table
Swift creates weather app
手持振弦采集仪对振弦传感器激励方法和激励电压
RedisUtil
CCF 201604-2 Tetris
SD/SDIO/EMMC
多数相合问题总结
salt常见问题
ISP image signal processing
See how a junior student of double non-2 (0 Internship) can get an offer from Alibaba and Tencent
oracle 解析同名xml 问题
无线振弦采集仪应用工程安全监测
SSM整合(简单的图书管理系统来整合SSM)
[Android studio] batch data import to Android local database
用ESP32+TM1638实验NTP网络校时闹钟的ARDUINO代码
Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification