当前位置:网站首页>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 ifelsewhilefor 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
边栏推荐
- Form verification of landline
- 1.6 example: cifar-10 classification
- 【C语言入门】ZZULIOJ 1031-1035
- (codeforce547)C-Mike and Foam(质因子+容斥原理)
- Inclusion exclusion principle
- Excel splicing database statement
- Target detection, industrial defects, image segmentation -- deep learning data set induction
- Code ~ hide or disable the status bar and virtual keys
- 2. Variables and scope
- lodash库常用方法
猜你喜欢

Shopify卖家:EDM营销就要搭配SaleSmartly,轻松搞定转化率

AI_ Drug: VAE of molecular generation model (I)

Ribbon principle analysis namedcontextfactory

Microcomputer principle and interface technology

Process tracking of ribbon principle

I.MX6U-驱动开发-2-LED驱动

(2022 Hangdian multi school III) 1002 boss rush (pressure dp+ dichotomy)

Raft protocol - process demonstration

Android view system and custom view Series 1: (kotlin version)

CUB_ Visualization of key points in 200 bird dataset
随机推荐
Notes on letter symbol marking of papers
Typescript from entry to mastery (XXI) generic types in classes
一文学透MySQL表的创建和约束
Division of data link layer, protocols used in data link layer and detailed introduction
How do programmers use code to completely end those things in the system?
深入C语言(3)—— C的输入输出流
华为天才少年稚晖君做了一把模块化机械键盘,引起极客圈地震,网友:这才是真正的客制化...
[redis series] string data structure
Rdkit II: use rdkit screening to screen 2D pharmacophores of chemical small molecules
Target detection, industrial defects, image segmentation -- deep learning data set induction
Sunflower senior product director technology sharing: "how to apply national remote control" in AD domain environment
(2022杭电多校三)1011-Link is as bear(思维+线性基)
大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
I.MX6U-驱动开发-2-LED驱动
Rdkit I: using rdkit to screen the structural characteristics of chemical small molecules
新零售O2O 电商模式解析
通过递归实现多级联动
How fast does it take to implement a super simple programming language?
Inclusion exclusion principle
Set functions in kotlin