当前位置:网站首页>[ES6] let, const, deconstruction assignment, template string
[ES6] let, const, deconstruction assignment, template string
2022-04-21 18:53:00 【Errol_ King】
List of articles
ES6 Study :
Silicon Valley Web front end ES6 course , cover ES6-ES11
ES6 Introductory tutorial Ruan Yifeng
let keyword
【 characteristic 】
let The keyword is used to declare variables , Use let Declared variables have several characteristics :
- Duplicate statements are not allowed ;
- Block level scope ( local variable );
- No variable promotion ;
- Does not affect the scope chain ;
【let Create variable code examples 】
// let Keyword usage examples :
let a; // A single statement
let b,c,d; // Batch statement
let e = 100; // Single declaration and assignment
let f = 521, g = 'iloveyou', h = []; // Batch declaration and assignment
【 Duplicate statements are not allowed 】
let name = " Zhang San ";
let name = " Li Si ";

【 Block level scope 】
Include if else while for
{
let age = 11;
//console.log(age);
}
console.log(age);

【 No variable promotion 】
var If you use variables first , Then declare and initialize it , The value of the variable will be undefined. The following code will be output on the console undefined
{
console.log(age);
var age = 18
}
and let This will report an error :
{
console.log(age);
let age = 18
}

【 Does not affect the scope chain 】
There is a code block inside the code block , Local variables in the upper code block are available to the lower level
let age = 18;
function fn(){
console.log(age); // Although it is a block scope , But here you can use
}
fn();
【let Case study : Click on div Change the color 】

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style> .item {
width: 100px; height: 50px; border: solid 1px rgb(42, 156, 156); float: left; margin-right: 10px; } </style>
</head>
<body>
<div class="container">
<h2 class="page-header">let Case study : Click on div Change the color </h2>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<script> // obtain div Element object let items = document.getElementsByClassName('item'); // Traverse and bind Events for (let i = 0; i < items.length; i++) {
items[i].onclick = function () {
// Modify the background color of the current element //this.style.background = 'pink'; // Writing a : The conventional writing method is generally normal items[i].style.background = 'pink'; // Write two } } </script>
</body>
</html>
const keyword
【 characteristic 】
const The keyword is used to declare constants ,const The statement has the following characteristics :
- Be sure to assign the initial value ;
- General constants use uppercase ( habit );
- Duplicate statements are not allowed ;
- Value cannot be modified ;
- Block level scope ( local variable );
- Modify an array or object element , No modification of constants , No mistake.
【const Code example 】
<script>
const SCHOOL = " Sanlitun primary school "
console.log(SCHOOL);
</script>
【 The declaration must be assigned an initial value 】
const SCHOOL;

【 Duplicate statements are not allowed 】
const SCHOOL = " Sanlitun primary school ";
const SCHOOL = " Sanlitun middle school ";

【 Value cannot be modified 】
const SCHOOL = " Sanlitun primary school ";
SCHOOL = " Sanlitun middle school ";

【 Block level scope ( local variable )】
{
const SCHOOL = " Sanlitun primary school ";
}
console.log(SCHOOL);

【 Modify an array or object element , No modification of constants 】
const PERSON = [' Zhang San ',' Li Si ',' Wang Wu ']
PERSON.push[' Zhao Liu ']
console.log(PERSON)

Deconstruct assignment
【 Definition 】
ES6 Allow to follow certain mode , Extract values from arrays and objects , Assign values to variables , This is called deconstruction assignment
【 Sample code 】
<script>
// 1、 Deconstruction and assignment of arrays
const F4 = [" Yan Chengxu ", " Zhu Xiaotian ", " Zhou Yumin ", " Wu Jianhao "];
let [a, b, c, d] = F4;
// This is equivalent to our statement 4 A variable a,b,c,d, Their values correspond to " Yan Chengxu "," Zhu Xiaotian "," Zhou Yumin "," Wu Jianhao "
console.log(a + " " + b + " " + c + " " + d);
// 2、 Object's deconstruction assignment
const person = {
name: " Zhang San ",
age: 18,
talk: function () {
console.log(" Hello , nice to meet you ");
}
}
let {
name, age, talk} = person;
console.log(" full name :" + name + " Age :" + age);
talk()
</script>

Of course, you can talk Method structure :
let {
talk} = person;
talk()
Template string
【 characteristic 】
Template string (template string) It's an enhanced string , Use back quotes (`) identification
- A newline character can appear in the string
- have access to
${xxx}Formal reference variable
【 Sample code 】
// The method of declaring a string : Single quotation marks ('')、 Double quotes ("")、 The quotation marks (``)
// Statement
let string = ` I also a string `;
console.log(string);
// characteristic
// 1、 A newline character can appear in the string
let str =
`<ul> <li> Yan Chengxu </li> <li> Zhu Xiaotian </li> <li> Zhou Yumin </li> <li> Wu Jianhao </li> </ul>`;
console.log(str);
// 2、 have access to ${xxx} Formal reference variable
let actor = " Yan Chengxu ";
let output = `${
actor} Played the role of Daoming temple `;
console.log(output);

版权声明
本文为[Errol_ King]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211848520066.html
边栏推荐
- 数据库进阶学习:索引介绍及Btree,B+tree,hash
- 【王道考研3】OSI七层参考模型,TCP/IP参考模型和5层参考模型
- 真无线耳机什么牌子好?高颜值旗舰蓝牙耳机
- Frida hook variable parameters
- Mysql database learning - Chapter 6 post class exercises of multi table query
- Tencent cloud database tdsql -- blog database migration practice
- URL转码问题:URLDecoder.decode(str)过时,解决:decode(String s, String enc) throws UnsupportedEncodingExceptio
- 工作总结!日志打印的15个建议
- Teach you how to build components and applications by hand
- 无线蓝牙耳机哪个品牌好?无线蓝牙耳机推荐
猜你喜欢

真无线耳机什么牌子好?高颜值旗舰蓝牙耳机

This thing is called a jump watch?

What's a good brand of real wireless headphones? High profile flagship Bluetooth headset

牛客 - 另类加法

Kotlin | 关于 Lazy ,你应该了解的这些事

SQL Server 数据库之 T-SQL 语言

CVPR2022 Oral | CosFace、ArcFace的大统一升级,AdaFace解决低质量图像人脸识

How to adjust the speed and speed of preview video in PR;

Daily question series: water bottle

如何查看redis源碼中的 zskiplist 結構
随机推荐
URL transcoding problem: urlcoder Decode (STR) is obsolete. Solution: decode (string s, string ENC) throws unsupportedencodingexceptio
美国IBM研究院Payel Das等人NMI论文:优化分子的通用型机器学习框架
JVM 类加载机制
【js学习笔记四十一】单体模式
Crystal Chem小鼠葡萄糖检测试剂盒说明书
86 R k-means,层次,EM聚类介绍
零知识证明的潜在价值
SVG系列——2,绘制一些形状
This thing is called a jump watch?
使用chrome的replay功能快速地发表一遍blog
[04] [01] [01] redis Basics
Use the replay function of chrome to publish a blog quickly
【短时幅度谱】短时幅度谱估计在语音增强方面的MATLAB仿真
LeetCode1765. Highest point in the map (BFS)
ViewPager中Fragment状态保存的哪些事
为什么你做数据分析没思路?
有什么蓝牙耳机不贵又实用?适合学生党的无线蓝牙耳机
如何检测PC上插了多少个摄像机?并进行多摄像头同步录制?
TypeScript快速上手,class,public,private,extends
无线蓝牙耳机哪个品牌好?无线蓝牙耳机推荐