当前位置:网站首页>ECMAScript 6(es6)
ECMAScript 6(es6)
2022-06-27 10:30:00 【大磊程序员(轻大)】
1、ECMAScript 6
1.1 什么是ECMAScript 6
ECMAScript 6.0(简称ES6)是JAvascript语言的下一代标准。它的目标,是使得Javascript语言可以用来编写复杂的大型应用程序,成为企业级开发语言。
1.2 ECMAScript 6和JAvascript的关系
ECMAScript 6和JAvascript的关系是,前者是后者的规格,后者是前者的一种实现(另外的ECMAScript 方言还有Jscript和ActionScript)
2、基本语法
2.1 let声明变量
//var 声明的变量没有局部作用域
//let声明的变量有局部作用域,只能用在对应的代码块中
{
//定义变量
var a=0
let b=2
}
console.log(a)//0
console.log(b)//b is not defined
//var 声明的变量可以被多次赋值
//let声明的变量只能被一次赋值
var m=1;
var m=2;
let n=10;
let n=20;
console.log(m) //2
console.log(n)//Identifier 'n' has already been declared
2.2 const声明常量
在es6中声明的常量必须赋值,并且不能再次赋值。
const a=0;
a=4;//Assignment to constant variable.
const b;//Missing initializer in const declaration
2.3 解构赋值
解构赋值是对赋值运算符的扩展。它是一种针对数组或者对象进行模式匹配,然后对其中的变量进行赋值。
在代码书写上简洁且可读,语义更加清晰明了;也方便了复杂对象中数据字段获取
数组解构
传统:
let a=1,b=2,c=3;
console.log(a,b,c)
ES6:
let[x,y,z]=[1,2,3]
console.log(x,y,z)

对象解构
传统:
let user={name:'Helen',age:18}
let name1=user.name
let age1=user.age
console.log(name1,age1)
ES6:
let{name,age}=user //注意:结构的变量必须是user中的属性
cosnole.log(name,age)

2.4 模板字符串
模板
字符串相当于加强版字符串,用反引号`,除了作为普通字符串,还可以用来定义多行字符串,还可以在字符串中加入变量和表达式
字符串中插入变量和表达式。变量名写在${}中,${}中可以放入JavaScript表达式。
let name="lucy"
let age=20
let info=`My name is ${name} ,
I am ${age+1}`
console.log(info)
2.5 声明对象简写
//传统定义对象
const name="lucy"
const age=18
const user1={name:name,age:age}
console.log(user1)
//es6
const user2={name,age}
console.log(user2)

2.6 对象扩展运算符
扩展运算符(...)用于取出参数对象所有可遍历属性然后拷贝到当前对象
//1、拷贝对象
let person1={name:"Amy",age:18}
let someone={...person1}
console.log(someone)
//2、合并对象
let age={age:18}
let name={name:"Amy"}
let person2={...age,...name}
console.log(person2)

2.7 箭头函数
箭头函数多用于匿名函数的定义
//传统
var f1=function(a){
return a
}
console.log(f1(1))
//ES6使用箭头函数定义
//参数=>函数体
var f2=a=>a
console.log(f2(1))
var f3=function(m,n){
return m+n
}
console.log(f3(1,2))
var f4=(m,n)=>m+n
console.log(f4(1,2))

边栏推荐
- Reorganize common shell scripts for operation and maintenance frontline work
- C any() and aii() methods
- [从零开始学习FPGA编程-47]:视野篇 - 第三代半导体技术现状与发展趋势
- 20 jeunes Pi recrutés par l'Institut de microbiologie de l'Académie chinoise des sciences, 2 millions de frais d'établissement et 10 millions de fonds de démarrage (à long terme)
- Exception in Chinese character fuzzy query of MySQL database
- Leetcode to do questions
- 用户认证技术
- torchvision. models._ utils. Intermediatelayergetter tutorial
- Write it down once Net analysis of a property management background service stuck
- JS client storage
猜你喜欢
Eureka核心源码解析

通俗易懂理解樸素貝葉斯分類的拉普拉斯平滑

导师邀请你继续跟他读博,你会不会立马答应?
![[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)](/img/6d/8b1ac734cd95fb29e576aa3eee1b33.png)
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)

Learning notes - data set generation

Feedforward feedback control system design (process control course design matlab/simulink)

NVME2.0协议——新特性

如何在 Methodot 中部署 JupyterLab?

【TcaplusDB知识库】Tmonitor单机安装指引介绍(一)
![leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]](/img/70/3954b0871cc31d24ae016eb99d871e.png)
leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]
随机推荐
JS all network request modes
Use of bufferedwriter and BufferedReader
【云享新鲜】社区周刊·Vol.68-华为云招募工业智能领域合作伙伴,强力扶持+商业变现
C language learning day_ 04
Future & CompletionService
闭包的常见问题
CPU设计(单周期和流水线)
【TcaplusDB知识库】TcaplusDB新增机型介绍
Easy to understand Laplace smoothing of naive Bayesian classification
[tcapulusdb knowledge base] tcapulusdb cluster management introduction
用户认证技术
go-zero微服务实战系列(七、请求量这么高该如何优化)
Advantages and disadvantages of distributed file storage system
Learning notes - data set generation
2-4 installation of Nessus under Kali
C語言學習-Day_04
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (II)
Write it down once Net analysis of a property management background service stuck
DNS standby server information, DNS server address (how many DNS preferred and standby are filled in)
Go zero micro Service Practice Series (VII. How to optimize such a high demand)