ES6 new syntax of let and const (one)

variable declarations : let, const,and block scope

why we redefine the way about declarations?

function scope

var price = 10;  //global declaration

function showPrice(){
    var price = 12; //local declaration using var
    console.log(price); //12
}
showPrice();

console.log(price);  //10

The next we can use IIFE to check var variable;

var price = 10;
(function(){
    var price = 12;
    console.log(price); //12
})();
console.log(price);  //10

IIFE: Immediately Invoked Function Expression,意为立即调用的函数表达式,也就是说,声明函数的同时立即调用这个函数。

How to change the scope of var variable?

var price = 10;
if(price){
    price = 12;
    console.log(price); 
}
console.log(price);

The examples demonstrates _ of var.

The example make me confusing.

var price = 10;

function showPrice(){
    var price = 12;
    console.log('price',price);
}

showPrice();
console.log('showPrice',showPrice());
console.log(price);

the resule is

price 12

price 12

showPrice undefined

10

why have two price?

we add block scope like let and const to solve the problems.

what is block scope?

block scoping means that new scope is created between a pair of { }.

let nbr = 12;
{
    let nbr = 40;
}
console.log(nbr)

1.var is bound to function scope
2.let and const are block scope

How to install preview about markdown in sublime?

1.we can use package install to install our plugins

.first use keyboard open the package istall

command+shift+p

IIFE

var price = 12;
if(price){
    var price = 10;
    console.log(price);
}
console.log(price);

IIFE tell us that the var declarations are bound to the function scope and does not create block scope.

var is bound to function scope, let and const are block scope.

let value = 42;
{
    let value = 1000;
}
console.log(value);

let varible only read in the block scope.

const

const declarations is a immutable varible.

const value  = 50;
console.log(value);

value = 1000;
const value  = 50;
console.log(value);

let value = 1000;

varible hoisting

console.log(host);
var host = 89;

In my heart,it may be console 89;Actualy it is undefiend;
beacuse it is become next code in our browser

var host;
console.log(host);
var host = 89;

If you use let to declate a varible,it is err:

console.log(host);
let host =100;

I'm confusing!

TDZ(Temporal Dead Zone)

You are accessing a varible that's been declared but not yet initialized.

let data = true;
if(true){
    console.log(data);
    let data;
} 
console.log(data);

It's print the value

undefiend
true;

But in the book ,author told me that print

ReferenceError
true

let data = true;
if(true){ //Enter new scope,TDZ starts
    //uninitialized bindling for data is created
    console.log(data); //ReferenceError
    let data;//TDZ ends,'data' is initialized with undefined
}
console.log(data); //true

TDZs helps us ensure that a variable in runtime always have correct value.

if(ture){
    console.log(typeof anUndeclaredVariable);
    console.log(typeof nrandom);

    let random;
}

It is a good practice to always make varible declarations at the top of your scope.

This check is also useful for conditionally creating global varibles using var.
You can check if a global variable exsits by doing something like this:

if(typeof globalVariable === 'undefined'){
    var globalVariable = {...};
}

const in object

You can add a property to object of const declaration but you cannot assign
a different value to object.

const obj = {};
obj.key = 42;
console.log(obj.key);

obj = {};

sure,if you really want to you could make the value itself immutable by freezing it.

const obj = Object.freeze({});
obj.key = 42;
console.log(obj);

Object.freeze() is shallow.

转载于:https://www.cnblogs.com/InnerPeace-Hecdi/p/8834883.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值