let 和 const 命令
let
{
let i = 1;
var j = 2;
}
console.log(i)
console.log(j)
for(let i = 0; i < 10; i++){
}
i
for(var i = 0; i < 10; i++){
}
i
var a = []
for(var i = 0; i < 10; i++){
a[i] = function(){
console.log(i)
}
}
a[5]()
for(let i = 0; i < 10; i++){
a[i] = function(){
console.log(i)
}
}
a[7]()
var array = []
for(var i = 0; i < 10; i++){
(function(i){
array[i] = function(){
console.log(i)
}
})(i)
}
array[7]()
for(let i = 0; i < 3; i++){
let i = 'abc'
console.log(i)
}
console.log(bar)
var bar = 2
console.log(bar)
let bar = 2
- 暂时性死区(在代码块内,let声明变量之前,该变量不可用,不会受外部的影响)
tmp = '222'
if(true){
tmp = 'abc'
let tmp
}
tmp = '222'
if(true){
tmp = 'abc'
console.log(tmp)
let tmp
console.log(tmp)
tmp = 123
console.log(tmp)
}
typeof x
let x
typeof aaaaaa
function bar(x = y, y = 2){
return [x,y]
}
bar()
function bar(x = 2, y = x){
return [x,y]
}
bar()
var x = x;
let x = x;
function foo(){
var a = 10;
var a = 20;
}
function foo(){
var a = 10;
let a = 20;
}
function foo(){
let a = 10;
let a = 20;
}
function fn(arg){
let arg
}
fn()
function fn(arg){
{
let arg
}
}
fn()
const(指变量指向的内存地址的值不可修改,如果定义复合类型只是指针不可修改,不能保证对象结构改变)
const aaa = 111
aaa = 222
const bbb
bbb = 111
if(true){
const eee = 666
}
console.log(eee)
console.log(ddd)
const ddd = 100
let bar = 100
const bar = 200