当前位置:网站首页>【ES6】let、const关键字和解构赋值
【ES6】let、const关键字和解构赋值
2022-07-13 18:28:00 【苏凉.py】
作者简介:苏凉(专注于网络爬虫,数据分析,正在学习前端的路上)
博客主页:苏凉.py的博客
系列专栏:ES6基础语法
名言警句:海阔凭鱼跃,天高任鸟飞。
要是觉得博主文章写的不错的话,还望大家三连支持一下呀!!!
关注点赞收藏
文章目录
es介绍
ES全称EcmaScript,是脚本语言的规范,而平时经常编写的JavaScript,是EcmaScript的一种实现,所以ES新特性其实指的就是JavaScript的新特性
为什么要学习新特性?
- 语法简洁,功能丰富
- 框架开发应用
- 前端开发职位要求
为什么要学习es6
- ES6 的版本变动内容最多,具有里程碑意义
- ES6加入许多新的语法特性,编程实现更简单、高效
- S6是前端发展趋势,就业必备技能
let关键字
let关键字与var关键字一样是用于声明变量的,只不过与var有以下不同之处:
变量不能重复声明
在es6中let关键字不允许重复声明变量,如下图所示就会报错:Uncaught SyntaxError SyntaxError: Identifier 'name' has already been declared
let name ='suliang';
let name = 'xiaoming';
块级作用域
使用let在代码块中声明变量时,只在代码块中生效。
{
var name = 'su'
let age = 21;
}
console.log(name); //su
console.log(age); //ReferenceError: age is not defined
不存在变量提升
使用var关键字定义一个变量时,可以在定义变量之前输出该变量,数据类型为未定义,但使用let关键字定义变量时,不允许在声明变量前调用该变量。
console.log(age); //ReferenceError: Cannot access 'age' before initialization
let age = 21;
不影响作用域链
function fun1(){
let num = 10;
function fun2(){
let num1 = num +15;
console.log(num1);
}
return fun2();
}
fun1(); //25
let关键字小案例
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script> //let 案例 window.onload = function(){
let items = document.getElementsByClassName('item'); for(let i = 0;i<items.length;i++){
items[i].onclick = function(){
items[i].style.backgroundColor = 'yellow' } } } </script>
<style> div{
width: 150px; height: 80px; border: 2px solid lightblue; display: inline-block; } </style>
</head>
<body>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</body>
</html>
当使用for循环遍历元素时,使用var关键字声明变量i,则会报错:TypeError: Cannot read properties of undefined,因为此时var 声明的i并没有块级作用域,最后i的值为3,而item[3]不存在,因此就会报错。而用let可以解决这个问题。
window.onload = function(){
let items = document.getElementsByClassName('item');
for(var i = 0;i<items.length;i++){
items[i].onclick = function(){
items[i].style.backgroundColor = 'yellow'
}
}
}

const关键字(声明常量)
在es6中怎加了对常量的声明方式,那就是用const关键字,其中使用const关键字,有以下特性。
声明时必须赋予初始值
在声明常量时必须赋予初始值,否则报错:SyntaxError: Missing initializer in const declaration
声明常量时尽量使用大写字母
const NAME = 'suliang';
常量的值不能被修改
常量的值一旦定义就不能被修改,否则报错:TypeError: Assignment to constant variable.
const NAME = 'suliang';
NAME = 'xiaoming';
块级作用域
和let一样,const一样有块级作用域
{
const AGE= 15;
}
console.log(AGE)
//ReferenceError: AGE is not defined
对于数组/对象的元素来说,不算对常量的修改
const LIST = ['gala','xiaohu','wei','ming'];
LIST.push('breath');
console.log(LIST);
//(5) ['gala', 'xiaohu', 'wei', 'ming', 'breath']
解构赋值
在es6中可以将一个常量中的各个值赋给多个变量。
数组的解构赋值
const RNG = ['xiaohu','ming','gala'];
let [a,b,c] = RNG;
console.log(a); //xiaohu
console.log(b); //ming
console.log(c); //gala
对象的解构赋值
最常用的通常为对象的解构赋值,这样调用对象的方法时更简便。
//对象的解构赋值
const su = {
name:'suliang',
age : 21,
sayName:function(){
console.log(name)
}
}
let {
name,age,sayName} = su;
console.log(name); //suliang
console.log(age); //21
console.log(sayName); //ƒ (){console.log(name) }
//调用对象方法
su.sayName(); //不适用解构赋值
sayName(); //使用解构赋值
边栏推荐
- What is fastmixer cash virtual currency encryption mixer?
- Wechat classroom reservation of applet completion works applet graduation design (3) background function
- 接口Mock详解及使用
- 杰理之按键切换默认 EQ 和 EQ 工具调节的 EQ【篇】
- 杰理之串口用 DMA 方式接收数据注意事项【篇】
- (2.4)【服务型木马-SlimFTP】介绍、使用
- 研发中台拆分过程的一些心得总结
- 深度揭秘阿里云函数计算异步任务能力
- C primer plus learning notes - 4. File IO (input / output)
- Functional model
猜你喜欢

函数式模型

ora-01153

From the perspective of global value chain, how will JD cloud digital intelligence supply chain affect the future economy?

小程序毕设作品之微信企业公司小程序毕业设计(3)后台功能

mySQL上的应用了解

一个XML文件例子

Wechat classroom appointment of applet completion works (1) summary of development

小程序毕设作品之微信企业公司小程序毕业设计(7)中期检查报告

2022-07 Microsoft vulnerability announcement

Cat and dog classification - simple CNN
随机推荐
命令提示符查看某端口占用情况,并清除占用
杰理之按键切换默认 EQ 和 EQ 工具调节的 EQ【篇】
Use cpolar to build a commercial website (apply for website security certificate)
一、mysql的安装部署
LeetCode 735 行星碰撞[栈 模拟] HERODING的LeetCode之路
一位 sealer maintainer 的心路历程
After some experiments, the metaphysics of batch size was broken
央行:我国绝大部分中小银行的央行评级处于安全边界内
2022-07 Microsoft vulnerability announcement
Mallbook: how to promote the rapid implementation of supply chain finance through the supply chain settlement management system?
HJ3 明明的随机数 HJ03
【每日一题】判断是不是平衡二叉树
(手工)【sqli-labs27、27a】报错回显、布尔盲注、过滤后注入
接口Mock详解及使用
CVPR | self enhanced unpaired image defogging based on density and depth decomposition
小程序毕设作品之微信企业公司小程序毕业设计(1)开发概要
From the perspective of global value chain, how will JD cloud digital intelligence supply chain affect the future economy?
CVPR | 基于密度与深度分解的自增强非成对图像去雾
Small program graduation project of wechat enterprise company (7) Interim inspection report
送你的代码上太空,一起开发“最伟大的作品”