ES6 article understand var let const difference easing the bit face questions trio

Written at the beginning

This article will take you to understand the difference between these three var let const statement, but also a point often test the interview.

Subsequent articles will be related to the front end, to welcome you all can talk about the road with people who, March is the beginning of the effort, come on!

Recommended reading: from the front face of the rookie practice of manufacturers by spring recruit interns

The difference between variables and constants

Before understanding the three variables, simple to talk about the difference between variable and constant

  • Variables: you can always change
  • Constant: Once defined, can not be changed

Ask a question 1

Here we look at this chestnut:

A use var defines a variable time, and then wrote out a method, and later came intern C, also defines a time with var, then it will be a bug?

//author A
var time = null;
doucument.onclick = function(
	time=set...
)
//author C
var time =function(){
	//
}

Apparently, two people with var lead to coverage problems, then A will be a bug, might run its own program is not up

Let statement introduced

We think, why ES6 let it be introduced? Of course, the problem is solved before ah ( This is not nonsense Well ) ok, chestnuts from above that we know can be repeated var statement, then let it be? (have a test)

let a = 59;
let a = 60;
console.log(a);

Sure enough, the console error, then we now know let it be allowed to repeat declared

But let's assignment is repeated, we might execute the code below

let a = 59;
a = 60;
console.log(a);

Introducing block-level scope

what? You do not know this stuff block-level scope? That article for some help.

We briefly mention scopes, before ES6, our common is the following situation:

This is a simple scope, we can not access external variables inside a function

function f(){
	var a=6;
}

So, block-level scope What do you mean?

{
	console.log(666);
}

You will feel amazing? Yes, it is this block-level scope!

Variable leak

Before ES6, we may have heard the variable leak, come, give chestnuts and guess the following results:

for(var i=0;i<10;i++){
	
}
console.log(i);

Obviously, we actually have access to the external value of our variable i

But, if we let this variable to declare it?

for(let i=0;i<10;i++){
	
}
console.log(i);

So, we let statement, you can not get outside variables

further verification:

for(let i=0;i<10;i++){
	console.log(i);
}
console.log(i);

The introduction of const statement

学习了 let 之后,我们知道了 let 不可以重复声明,可以重新赋值,还可以防止变量泄露

那么,我们简单讲讲const把

const a = 10;
a=20;
console.log(a);

我们发现,const 就是定义了一个常量,相当于定s,不允许重复声明,不允许重新赋值,但const 是不能防止变量泄露的,因为值已经定住了。

const 的应用呢,要说的话比如写node的时候,会有如下代码,简单提一下:

const fs = require('fs');

总结

let - 变量 不允许重复声明 允许重复赋值 可以防止变量泄露
const - 常量 不允许重复声明,也不允许重复赋值

学如逆水行舟,不进则退
发布了548 篇原创文章 · 获赞 1584 · 访问量 24万+

Guess you like

Origin blog.csdn.net/weixin_42429718/article/details/104589336