当前位置:网站首页>Let variable declaration feature of ES6 new feature and its case
Let variable declaration feature of ES6 new feature and its case
2022-07-29 03:45:00 【Haha, APE】
let The keyword is used to declare variables , Use let Declared variables have several characteristics :
- Duplicate statements are not allowed
- Has block level scope ( local variable )
- No variable promotion
- Does not affect the scope chain
Code demonstration :
1. Duplicate statements are not allowed
// 1. Variables cannot be declared repeatedly
let beautifulGirl = " Xiao Liu "
let beautifulGirl = " Classmate Liu "
// Report errors :Cannot redeclare block-scoped variable 'beautifulGirl'
2. Block level scope ( local variable )
{
var girl = " Xiao Liu "
}
console.log(girl);
// Print the results : Xiao Liu
{
let girl = " Xiao Liu "
}
console.log(girl);
// Report errors :Uncaught ReferenceError: girl is not definedat
Be careful : stay if
else
while
for
Use in let
It's all block scope
3. No variable promotion
Use var
( There is variable promotion )
console.log(girl);
var girl = " Xiao Liu "
// Print the results :undefined
Use let
( No variable promotion )
console.log(girl);
let girl = " Xiao Liu "
// Report errors :ncaught ReferenceError: Cannot access 'girl'
notes : Variable promotion is used before variable creation ( Like output : The output is the default value ),let
non-existent ,var
There is
4. Does not affect the scope chain
{
let beautifulGirl = " Xiao Liu ";
function fn(){
console.log(beautifulGirl);
}
fn()
// Print the results : Xiao Liu
}
notes : Scope chain is a code block with code blocks , Just like regular programming languages , Local variables in the upper code block are available to the lower level
let Use cases :
html Code
<style>
.item {
width: 100px;
height: 50px;
border: solid 1px rgb(42, 156, 156);
float: left;
margin-right: 10px;
}
</style>
<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>
</body>
JavaScript Code
// obtain class be known as item The elements of
let items = document.querySelectorAll(".item")
// Traverse binding elements
for(let i=0;i<items.length;i++){
items[i].onclick = function(){
// Modify the background color of the current element
// Writing a
// this.style.background = 'pink'
// Write two :
items[i].style.background = "pink"
}
}
Write 2 pay attention to events
stay for
Circulation is used i
You have to use let Statement
If you use var
You're going to report a mistake ( reason :var
Global variable )
After the cycle i The value of will become 3,items[i]
The following table will be crossed let
It's a local variable
We need to know when clicking i That's the value.
// Use var Equivalent to :
{ var i = 0; }
{ var i = 1; }
{ var i = 2; }
{ var i = 3; }
// The following statement will overwrite the above , So every time you click on an event, you find 3
// While using let Use var Equivalent to :
{ let i = 0; }
{ let i = 1; }
{ let i = 2; }
{ let i = 3; }
// because let Local variables are declared , Each one keeps its original value
// When you click the event call, you get the corresponding i
summary :let Declared variables are more standardized and reasonable , Use as much as possible let To declare and use variables
边栏推荐
- (nowcoder22529C)dinner(容斥原理+排列组合)
- 最新二开版漫画小说听书三合一完整源码/整合免签接口/搭建教程/带采集接口
- The list is not updated in real time when JS V-for data changes
- (newcoder 15079) irrelevant (inclusion exclusion principle)
- CUB_ Visualization of key points in 200 bird dataset
- 大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
- Precautions for using latex
- for_ Example of each usage
- Simple understanding of CDN, SDN and QoS
- (2022 Hangdian multi school III) 1002 boss rush (pressure dp+ dichotomy)
猜你喜欢
I.MX6U-驱动开发-2-LED驱动
Shutter start white screen
Multi level wavelet CNN for image restoration
Flutter 启动白屏
Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing
Getting started with caspin
EMD 经验模态分解
EMD empirical mode decomposition
Solve the delay in opening the console of Google browser
Simple code implementation of decision tree
随机推荐
Sunflower senior product director technology sharing: "how to apply national remote control" in AD domain environment
CUB_ Visualization of key points in 200 bird dataset
Cloudera manager platform fault repair record
【redis系列】字符串数据结构
"Strangers once met" Summer Street Shen Shuyan_ Xia Mo Shen Shuyan's latest chapter
Remote desktop connection error
深入C语言(4)——switch的定义与使用
RTP send and receive h265
MOS tube - rapid recovery application notes (II) [parameters and applications]
In depth C language (4) -- definition and use of switch
The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
three. JS Part 54 how to pass structure array to shader
Rdkit I: using rdkit to screen the structural characteristics of chemical small molecules
无法一次粘贴多张图片
Simple use of eventbus
Tristate gate
Data too long for column 'xxx' at row 1 solution
EMD empirical mode decomposition
3.解决Pycharm报错Unresolved reference ‘selenium‘ Unresolved reference ‘webdriver‘
Alibaba Font Icon Library Usage and update methods