当前位置:网站首页>Write blog at leisure ~ briefly talk about let, VaR and Const
Write blog at leisure ~ briefly talk about let, VaR and Const
2022-07-24 06:35:00 【LuciferDawnC】
Catalog
1. Variable
let and var Is a data storage container that declares variable types
What is a variable ? Variable The saved value is variable. The saved parameter value can be changed and re assigned according to the needs , such as
var num1 = 1;
console.log(num1); // 1
num1 = num1 + 1;
console.log(num1); // 2
let num2 = 1;
console.log(num2); // 1
num2 = num2 + 1;
console.log(num2); // 2Are these all right ?
This is within the scope of normal system operation
Then it's all variable declarations. What's the difference between them ?
1.1 Repeated statement
var a = 1; // a = 1
a = 2; // a = 2
// Normal operation, right ? No problem, right ? I change its internal value
var a = 1; // a =1
// Then when I continued to write the code, I forgot that I stated a 了 , I made it clear again a~ At this time ~ The system says no problem ! You declare that I will store it ! Old fellow iron is not defective.
let a = 1; // a = 1
a = 2; // a = 2
let a = 1; // Uncaught SyntaxError: Identifier 'a' has already been declaredYou see you see Why is this wrong ? Identifier declared ? This is the legendary statement that cannot be repeated ~let Declared variable containers cannot be declared repeatedly or ~ You see, I was reminded ?let This variable will be protected during and after declaration Prevent parameter confusion in repeated declarations, that is ~
var a = 1; // a = 1
let a = 1; // no way
let a = 1; // a = 1
var a = 1; // Not good either. !!!!1.2 Scope
stay js In the code
let a = true
if(a) {
var j = a;
let i = a;
}
console.log(j) // true
console.log(i) // i is not definedThe same execution declares the output ! Why is the result so different ? stay js in es6 Declarative grammar let It is also called block level scope declaration , Brace scope declaration . all if for function In the sentence let Declarations cannot be read externally ! and var stay if for After the declaration in, the external can still read , Only function in var Variables of will have the effect of scope isolation !
1.3 Variable Promotion
What is the promotion of variables ? As the name suggests, the promotion of variables is to promote variables !
console.log(a); // a is not defined
// Output an undefined variable It will prompt that it is undefined ! This is the detection function in the system to remind us that there is a problem in this place ! Buddy Change it ?
var b;
console.log(b); // undefined
// undefined A variable that declares that it is not copied will be defined as undefined! Why not not defined? Because this variable is declared ,a is not defined It's a false report undefined It's output b Value It's just b The value of is undefined only ~ Let's go on
console.log(c); // undefined
var c = 0;
// This ! It's stated Also assigned Why is his result still // undefined ? At least he should give me a not defined!! Why is it like this ? This involves the promotion of variables !
// stay js When the code is executing , The system will read the code first . When reading the variable declaration, the declaration will be promoted to the head . That is to say, this variable has been declared at the time of output ~ Just write in the back var c = 0; This one is just assigned
console.log(c); // undefined
var c = 0;
// In fact, it is equivalent to
var c;
console.log(c); // undefined
c = 0;
// In this way ! Just look at it like this ?
// and let Would have been different
// console.log(d); // Cannot access 'd' before initialization
let d = 0;
console.log(d); // 0
// Ah ~ by the way I just don't want to be promoted ! The system read me ? Go to school ~ Let me improve ? I don't do it ~ Now I declare Then I need to use it after that ~ I just listen to the programmer's father and stay here ~ So the system told you You use this before initialization ! You have problems with these ~ Check the block ~ Um. ~ That's it ~ By the way ~ When an error is reported, the following console.log Can't output ~
We often state
2. Constant
consot Is a data storage container for declaring constant types
What is a constant ? constant Immutable value It is often used in the immortal world …… Set the number !!
const num3 = 1;
console.log(num3); // 1
num3 = num3 + 1;
console.log(num3); // Uncaught TypeError: Assignment to constant variable.It's said to be a fixed number You still want to change it ~ My fate depends on me, right ? How to know whether it's OK without trying ? Now I'm wrong ? Constants cannot be reassigned ! The value stored inside it is immutable But available Computable It is a common read-only type in computers Value can read it Don't try to change it ~
Don't think about it ?emm It seems that how to say ~
const num4 = {a: 1};
console.log(num4.a); // 1
num4.a = 2;
console.log(num4.a); // 2How has it changed ? It doesn't mean you can't become ?
This is because {a:1} Is the reference data type What this data type stores in variables is actually the address value corresponding to the data stored in stack memory , We are changing one of these address values a Attribute time constant num4 The saved address value is ' unchanged ' Of Become just in the object a Value . This may be a small potential that can be changed , Don't go against the general trend ~ The system only pays attention to the physical address value stored in constants during operation And for changes in address values It is not concerned ! So as long as the constant is not
const num4 = {a: 1};
num4 = {a: 1};This operation of repeated assignment is ok You say he is still equal to the original object ~ No, there is only one {a: 1} Do you ? sorry Even if the value inside is exactly the same It is also two different objects in the computer ! This involves reference data types, basic data types and stack storage emmm Write it in the following article
边栏推荐
- Leetcode sword finger offer jz73 flip word sequence
- go语言的快速上手
- Custom MVC 1.0
- awk的使用
- openssl版本升级
- 力扣986.区间列表的交集
- 常用工作方法总结(7S、SWOT分析、PDCA循环、SMART原则、6W2H、时间管理、WBS、二八原则)
- Solution of forgetting root password in mysql5.7 under Windows
- Server hardware and RAID configuration practice
- Leetcode sword finger offer jz25 merges two sorted linked lists
猜你喜欢
随机推荐
IP course (OSPF) comprehensive experiment
MySQL Index & execution plan
Flink production environment configuration recommendations
Work summary of a test Manager / Test Supervisor / test director
测试经理/测试组长/测试主管面试题
IP notes (12)
leetcode剑指offer JZ3 数组中重复的数字
迭代器与生成器
MeterSphere一站式开源持续测试平台
【217】#!/ The meaning of usr/bin/env
Custom MVC 2.0
MySQL数据库—SQL汇总(记得关注我!中国加油!)
文件系统与日志分析
TensorFlow-GPU 安装 -- 056
Unable to boot after permanent mounting
Login page + summary
手动安装Apache
Do not rent servers, build your own personal business website (2)
数据集和预训练模型
Custom MVC 1.0








