当前位置:网站首页>The difference between let and VaR
The difference between let and VaR
2022-07-19 01:37:00 【Still】
1.var
1.1 var Assignment
var message
Defines a name message The variable of , You can save any type of value ( Without initialization , The variable holds a special value undefined).
var message = 'hi'
to message After the assignment , It can not only change the saved value , You can also change the type of value .( Not recommended )
1.2 var Declaration scope of
Use var The variable defined by the operator becomes a local variable of the function that contains it . such as , Use var Define a variable inside a function , This means that the variable will be destroyed when the function exits .
function test() {
var message = "hi"; // local variable
mess = "hi"; // Global variables
}
test()
console.log(message,mess) // message Report errors ,mess Output successful
1.3 var Statement promotion of
The variable declared by the keyword will be automatically promoted to the top of the function scope ,var There is no problem repeating the statement .
function foo() {
// var age The statement is elevated here
console.log(age);
var age = 26;
}
foo(); // undefined There's no mistake here , because age Your statement was promoted .
2.let
2.1 let There is no claim to promote ( Temporary dead zone )
stay let The moment of execution before the declaration is called “ Temporary dead zone ”(temporal dead zone), At this stage, any reference to a later declared variable is thrown ReferenceError
console.log(message) // Report errors
let message = "hi"
2.1 let Declaration scope of
let And var The big difference ,let The scope of the declaration is the block scope , and var The scope of the declaration is the function scope .let Duplicate statements are not allowed .
Function scope > Block scope
// if Is block scope , Non functional scope ,var Out of the scope of the block will not be destroyed .
if (true) {
var name = 'Matt';
console.log(name); // Matt
let age = 26;
console.log(age); // 26
}
console.log(name); // Matt
console.log(age); // ReferenceError: age No definition
3. Global declarations
Use let Variables declared in the global scope do not become window Object properties (var Declared variables will )
var name = 'Matt';
console.log(window.name); // 'Matt'
let age = 26;
console.log(window.age); // undefined
4.let and var Of for Use
Yes, it is var Definition for The iteration variables of the loop will penetrate outside the loop body
for (var i = 0; i < 5; ++i) {
// Circular logic
}
console.log(i); // 5
var There is no block scope
for (var i = 0; i < 5; ++i) {
setTimeout(() => console.log(i), 0)
}
The actual output 5,5,5,5,5
for (let i = 0; i < 5; ++i) {
setTimeout(() => console.log(i), 0)
}
The actual output 1,2,3,4,5
because var No block level scope ,JS When synchronous and asynchronous code exist at the same time , Asynchronous code will call after all synchronous code is executed . and setTimeout It's asynchronous code ( Even if the delay is zero, it is asynchronous ), It will execute at the end of the code . So it is for After the loop is executed , Go again settimeout().
Also, if in for Nested in a loop function(), Programs are executed sequentially , therefore function This complex data type will exist in the heap , When for After the loop executes, the function starts to be called .
The function is executed immediately , This can be avoided
<button> Button 1</button>
<button> Button 2</button>
<button> Button 3</button>
<button> Button 4</button>
<button> Button 5</button>
var btns = document.getElementsByTagName('button');
for(var i=0; i<btns.length; i++){
(function(num){
btns[i].addEventListener('click',function(){
console.log(' The first ' + num + ' A button is clicked ')
})
})(i)
}
边栏推荐
- 自己封装的风格化的开关卡片组件
- es6 map提取数组对象
- Differences between let and const, let, const and VaR
- 小程序嵌入网页向小程序跳转并传参,微信小程序中实现公众号授权获取openId
- Summary of Applied Cryptography
- Determine whether the two timestamps are the same day
- 【ElenmentUI el-date-picker日期选择器,结束时间不得早于开始时间,且只能选择距开始时间指定天数的日期】
- markdown编辑器语法——文字颜色、大小、字体与背景色的设置(转载)
- 解决小程序自定义底部菜单切换闪动
- ES6 syntax -- Deconstruction assignment
猜你喜欢

Cento7安装mysql5.5以及升级5.7

How does the website count the number of visitors? How to install and use 51la?

uni-app微信公众号(1)——网页授权登录

Summary of XML external entity injection (xxE target recurrence)

Quine injection of SQL injection

object-fit:cover; It doesn't work in the applet. How to deal with the deformation of the applet image

Differences between let and const, let, const and VaR

gtest与gmock的安装与使用

NodeJS 跨域 CORS

深拷贝与浅拷贝
随机推荐
Uni canvas intercepts pictures
JS get the suffix format of a file name
uni-app微信小程序——商城(8)——订单详情
2022年暑假ACM热身练习2(总结)
红日靶场2
mock平台的使用说明
JS数组常用方法
今天的码农女孩总结了jQuery处理缓存的方法和事件委托方法的区别的笔记
2022年暑假ACM热身练习1(总结)
Uni app wechat applet - Mall (3) - Mall Homepage
let和const、let、const和var的区别
Uni app wechat applet - Mall (8) - order details
Uni app wechat official account (4) - address management page
网站被黑,通过百度/搜狗等搜索关键词访问跳转到其他网站怎么办?
Timestamp conversion time
2022.7.7 一些错误总结
NodeJS 跨域 CORS
Use redis - Zset to make Leaderboard
Quine injection of SQL injection
promise