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.

ES6 new syntax of let and const (one)的更多相关文章

  1. JavaScript学习系列5 ---ES6中的var, let 和const

    我们都知道JavaScript中的var,在本系列的 JavaScript学习系列2一JavaScript中的变量作用域 中,我们详细阐述了var声明的变量的作用域 文章中提到,JavaScript中 ...

  2. ES6中不得不说的关键字const

    上一节讲了let关键字,它是用来声明一个变量,只在块级作用域起作用.这一节我们来学习ES6新增的另一个关键字const. const 的作用 const是constant(常量)的缩写,const和 ...

  3. es6学习笔记1 --let以及const

    let语句的基本用法:  1.let声明的变量为块级作用域,只在最近的{}里面有效,如果在外部引用就会报错. { let a = 10; var b = "hello" } ale ...

  4. ES6新特性:let和const的使用

    (声明, 本文的所有代码均在node的最新稳定版本v4.4.3中执行的, 如果在浏览器中执行请把JS的运行环境提升为ES6) 以前一直用var定义变量, 现在有了两种新的定义变量的方式, 1: let ...

  5. es6重点笔记:let,const

    一,let 先看代码: var a = []; for (var i = 0; i < 10; i++) { a[i] = function () { console.log(i) }; } a ...

  6. ES6系列之变量声明let const

    ES6也出来好久了,最近闲来无事就想着吧es6做一个系统的总结,巩固自己的知识,丰富一下博客. 为什么叫ES6 实际上是ECMA的一个打的标准,这个标准是在2015年6月发布的,正式的名字实际是es2 ...

  7. ES6中声明变量 let和const特点

    在ES6中我们有两种定义变量的方式:let    const let特点: 1.let定义时不会进行变量声明提升 2.变量不允许被重复定义 3.变量不可以被删除 4.在for循环当中用let定义i 循 ...

  8. es6 入坑笔记(一)---let,const,解构,字符串模板

    let  全面取代var 大概相似于C++的定义,一个变量必须得先定义后使用,没有预编译 注意let的作用域,一个{}就是一个作用域,上述规则须在一个作用于内 坑:for(let i =0;i < ...

  9. ES6基本语法之let和const

    1.var可以重复声明 var a = 12; var a = 5; alert(a) //5 2.var无法限制修改 如:PI = 3.1415: 3.var没有块级作用域 { } 像: if(){ ...

随机推荐

  1. Oracle安装11.2.0.4.180116补丁及如何检查数据库安装补丁

    最近做了一个安装11.2.0.4.180116补丁的实验,突然想起之前和同事讨论的一个问题:如何检查数据库安装补丁的版本,之前搜到的是去查dba_registry_history,有的说在操作系统中执 ...

  2. Java 小记 — RabbitMQ 的实践与思考

    前言 本篇随笔将汇总一些我对消息队列 RabbitMQ 的认识,顺便谈谈其在高并发和秒杀系统中的具体应用. 1. 预备示例 想了下,还是先抛出一个简单示例,随后再根据其具体应用场景进行扩展,我觉得这样 ...

  3. 解决Hystrix Dashboard 一直是Loading ...的情况

    Hystrix是什么 Hystrix 能使你的系统在出现依赖服务失效的时候,通过隔离系统所依赖的服务,防止服务级联失败,同时提供失败回退机制,更优雅地应对失效,并使你的系统能更快地从异常中恢复. Hy ...

  4. 真是没想到,ikvm.net居然停止开发了。

    看样子作者对.net已经失去了信心 http://weblog.ikvm.net/CommentView.aspx?guid=33ea525f-a291-418a-bd6a-abdf22d0662b# ...

  5. 多目标跟踪(MOT)论文随笔-SIMPLE ONLINE AND REALTIME TRACKING WITH A DEEP ASSOCIATION METRIC (Deep SORT)

    网上已有很多关于MOT的文章,此系列仅为个人阅读随笔,便于初学者的共同成长.若希望详细了解,建议阅读原文. 本文是tracking by detection 方法进行多目标跟踪的文章,在SORT的基础 ...

  6. 一起happy--C++小组Alpha版本发布说明

    1 功能介绍 该PC端APP,是一个同行者的信息搜索平台,旨在为喜欢游玩,但是身边同学朋友时间冲突,想找人结伴的年轻人提供一个检索平台,让他们尽量能够快速便捷的寻找合适同行者.该APP有登录.注册.主 ...

  7. 201621123050 《Java程序设计》第1周学习总结

    1.本周学习总结 java历史概述 java特点:1.简单 2.面向对象 3.健壮 4.跨平台 5.类库众多 JDK.JRE.JVM JDK:JAVA 开发工具包 ,包含JRE JRE: JAVA运行 ...

  8. RxSwift(一)

    文/iOS_Deve(简书作者) 原文链接:http://www.jianshu.com/p/429b5160611f 著作权归作者所有,转载请联系作者获得授权,并标注"简书作者" ...

  9. 解析与动作联动得SDN数据平面

    一种解析与执行联动的SDN可编程数据平面 现有问题和目标 在传统协议处理方式中,各层的协议类型和组合方式固定,使得添加或修改协议很困难(因为需要修改网络设备的解析模式) 基于解析和执行联动结构的可编程 ...

  10. 【技巧】Java工程中的Debug信息分级输出接口及部署模式

    也许本文的标题你们没咋看懂.但是,本文将带大家领略输出调试的威力. 灵感来源 说到灵感,其实是源于笔者在修复服务器的ssh故障时的一个发现. 这个学期初,同袍(容我来一波广告产品页面,同袍官网)原服务 ...