Understanding and let const from the project why it is so important

Variable declaration

Variable declaration ways

Accompanied by the birth of the var js

// 语法  var varName = value
var a = 1  // 这样子你就得到了一个变量

var defect scene analysis

var specialUser = "cj"; // 在A文件定义
 
var specialUser = "fk"; // A文件很大,你没去寻找是否定义此变量,直接定义

getImportantInformation(specialUser); // 在B文件定义

We define a variable, usually skip check whether this variable has been defined , especially in the preparation of local codes, it is even more likely to lead to the definition of duplicate names caused, resulting in some of the core variables are covered, causing significant damage to the system , in the above example will cause the system to use because spcialUser variable to the variable where all abnormal behavior, people should learn to be lazy, if the definition of this variable, the compiler can know , why should we go before the programmer to define variables, but also in project Find this variable is defined under the file if it, code is compiled, if you have defined this variable, direct conflict tells us that we needed to replace just fine .

js specification for a lot of trouble, let the birth of the

In fact, the cover off the core variables, I was met in the business, estimated number of programs ape encounter this kind of thing, then ECMAScript official came out, all js language interpreter, must implement a new way of variable declarations help us spend less time worrying cover variable.

let name = "cj"; // 无论在何处定义
let name = "cj"; // 如果此变量在此作用域找了let name 这样的,编译器就会说,你这个名字用过了,要换个名字。

nDrJln.png

If the variable is defined scope with a variable of the same name, the interpreter will tell you that you and others of the same name, you need to change the name, have let this we do not have to worry about the original variables are covered, you can rest assured that the definition of variables, Very comfortable.

More than that, var abnormal behavior

When we learn the variables, the teacher would say, first define a variable and then access, we admit defeat, and then try to access the definition of

console.log(name)  // 咦? 为什么不会报错
var name = "cj"

Ok? Js this time with the teacher teaches there is a conflict, yes, the teacher was wrong, but a counter-js common sense stuff, so why had designed it, is not known, but here, but increase the use of cost variables, and why do I say

code 1
code 2  //

var name = 1 // code 2 定义的变量,

One day we would like to refer to this variable in code1, whether this might be to search for variables defined in code1 before, this is to be able to properly access code1 name, well, you guessed it, we can let the compiler tool to help us deal with this matter, if we access variables before defining variables, so the compiler can not access notify us here

console.log(name)  
let name = "cj"

nDshD0.png

看到这熟悉的提示,喔,原来我们在定义变量之前访问了变量,这时候我们就修改一下代码调用顺序,其实,这种机制也保证了代码的显式调用顺序的健壮

console.log(name)   // 嗯? 不行?
let name = "cj"

console.log(name)  // 这样才对
let name = "cj"

不受控制的var

由于程序少则几百行,我们公司大就几十万行,这就出现了人的名字的管理问题,如何解决重名问题呢,在此基础上拓展新的标识是一个不错的方法,比如中国很多重名的,地址就是一个额外标记的好东西,我们程序也在内存里分块,但是这不是重点,在内存上一层的抽象,语言有一个叫做作用域的东西,相当于把程序分成不同的城市一般来说变量被限制在这些块里,通过这种方式,我们很多通用的变量名得以大量使用而不冲突,因为它们不属于一个块,想象一下,如果中国只有一个城市,那么同名的人就可能得叫支付宝-1 支付宝-2了,变量在这种情况下也是一样,需要大量前缀,所以作用域的作用可想而知,当然作用域的设计来源复杂多,嘻嘻,以后有空研究下,

if(condition) {
  var a = 11;
}
var a = 33  // 此时我想在if外面的作用域定义了一个变量,却以不小心覆盖了if内部的变量a,导致程序异常

上述代码还有致命问题,一般来说,我们在不同作用域定义变量是互不影响的,但是上面的代码却违反了这个规则,倒置变量可能的异常,上述代码在同步场景下,基本没什么影响,在异步场景就会因为后者定义覆盖的问题导致程序异常

if(true) {
  var a = 3
    setTimeout(  () =>  {
      console.log(a) //  5而不是3
    }, 0)
}
var a = 5

let 就使得js拥有了真正的块作用域

if(true) {
  let a = 3
    setTimeout(  () =>  {
      console.log(a) //  3
    }, 0)
}
let a = 5

let遗漏的场景

let PI = 3.14

PI = 3.14132

有时候我们需要定义一个变量,在很多地方使用,但是由于这个变量很重要,所以后续有对它的修改,都是会对程序造成破坏,我们不希望,但是,当我们对一个变量赋值时,怎么知道其是不能修改的呢,嗯,只能阅读源码,确定其重要性,这样费事费力,能让我们的工具人编译器帮助我们吗,是的,它做到了

const PI = 3.14
PI = 3.14132

nDcZ4S.png

When we want to modify some of the values, if the author has marked as const can not be modified using a variable, then we modify the compiler will tell us, hey, brother, moved this variable system will hang up, really great.

to sum up

In the new syntax environment, we use as much as possible let to define variables on this basis, if it is not necessary to change the variables, you can use const to define , on the one hand to avoid modifying others, one can also enhance the compiler effectiveness.

Guess you like

Origin www.cnblogs.com/sefaultment/p/11518271.html