当前位置:网站首页>Scope of ES6 variable
Scope of ES6 variable
2022-06-25 13:20:00 【acgCode】
JavaScript Update to ES6, Added a new let and const Two commands to declare variables . I combine var The scopes of these three commands are compared .
| Command name | Scope |
|---|---|
| var | overall situation |
| let | Block level |
| const | Block level |
In order to see the difference more directly , You can see the following code :
for (var var_i = 0; var_i < 3; var_i++) {
{
console.log(var_i);
}
}
// Global scope
console.log(var_i);
for (let let_i = 0; let_i < 3; let_i++) {
{
console.log(let_i);
}
}
// Block level scope
// console.log(let_i);
{
const CONST_I = "const";
console.log(CONST_I);
}
// Block level scope
// console.log(CONST_I);
The output is :
var: 0
var: 1
var: 2
var: 3
let: 0
let: 1
let: 2
const: const
You can see the source code for let and const Output out of block , I just commented it out . because let and const It's a block level scope , Output outside the block will directly report an error .
This feature can explain many problems , Like the ones that we see all the time for Loop binding events .
<!DOCTYPE html>
<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>let Scope and var Scope differences </title>
</head>
<body>
<style> .item {
width: 100px; height: 50px; margin: 20px; border: 1px solid #3b3b3b; } </style>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</body>
</html>
use let Traverse :
let items = document.getElementsByClassName('item');
for (let index = 0; index < items.length; index++) {
items[index].onclick = function () {
items[index].style.background = 'green';
console.log(`let: ${
index}`);
}
}
Click event Normal execution , And the output is as follows :
use var Traverse :
let items = document.getElementsByClassName('item');
for (var index = 0; index < items.length; index++) {
items[index].onclick = function () {
console.log(`let: ${
index}`);
items[index].style.background = 'green';
}
}
Click event unexecuted , And the output is as follows :
It can be seen from the above that ,var Because it is a global scope , So after the loop is completed, it becomes 3, So when the click event is executed , Find the top level of the variable index Located globally , Has become 3, Array overflow , Click event cannot be executed normally ; and let Because it is a block level scope , When the click event is executed , Find the highest level of the variable in the loop body , It is still corresponding index, Click the event to execute normally .
Flexible application of variable scope , Different development effects can be achieved . Under the rigorous development mode , It is recommended to use more let and const Such block level scopes , This makes the code more secure .
边栏推荐
- .NET in China - What's New in .NET
- Regular match the phone number and replace the fourth to seventh digits of the phone number with****
- 量化交易之回测篇 - 期货CTA策略策略(TQZFutureRenkoWaveStrategy)
- 时间过滤器(el-table)中使用
- Serevlt初识
- MySQL learning notes
- Openstack learning notes -nova component insight
- KDD 2022 | GraphMAE:自监督掩码图自编码器
- 关于一个图书小系统的实现
- An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
猜你喜欢
Module 5 (microblog comments)

leetcode - 384. 打乱数组

Some knowledge about structure, enumeration and union

An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down

À propos du stockage des données en mémoire

Maui's learning path (II) -- setting

字符串各操作函数与内存函数详解

剑指Offer 第 2 天链表(简单)

初始c语言的知识2.0

剑指 Offer II 032. 有效的变位词
随机推荐
Koa frame
And console Log say goodbye
字节跳动Dev Better技术沙龙来啦!参与活动赢好礼,限时免费报名中!
JVM parameter interpretation
[转]以终为始,详细分析高考志愿该怎么填
[pit avoidance refers to "difficult"] antd cascader implements new customized functions
剑指 Offer II 028. 展平多级双向链表
关于扫雷的简易实现
OpenStack学习笔记(一)
Openstack learning notes -nova component insight
用include what you use拯救混乱的头文件
与生产环境中的 console.log 说再见
时间过滤器(el-table)中使用
Django框架——缓存、信号、跨站请求伪造、 跨域问题、cookie-session-token
剑指 Offer II 025. 链表中的两数相加
golang键盘输入语句scanln scanf代码示例
《MongoDB入门教程》第01篇 MongoDB简介
量化交易之回测篇 - 期货CTA策略实例(TQZFutureRenkoScalpingStrategy)
关于一个图书小系统的实现
药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质