[ES6] 03. The let keyword -- 1
var message = "Hi";
{
var message = "Bye";
} console.log(message); //Bye
The message inside the block still has impact on the outside.
Just remember that something like for, while if ... block, they don't create new scope. function block do create new scope!
If you add function around the inside message:
var message = "Hi";
function greeting(){
var message = "Bye";
}
console.log(message);
//Hi
Then "Bye" message has no impact on the "Hi" message.
But if create something like "for", "while" loop and if block, you will still get the "Bye";
Let
To help with this problem, we do have LET in ES6, which will allow me to use block scoping.
let message = "Hi";
{
let message = "Bye";
} console.log(message); //Hi
This "Bye" message, because it's inside of a block, even though it's not inside of a function, has no impact on the assignment of this message. They are two separate and different entities.
So we con consider that "let" keyword will create a new scope in the current block!
Let in For Loop:
Recall one problem code:
var fs = [];
for(var i = 0; i < 10; i++){
fs.push(function(i){
console.log(i)
});
}
fs.forEach(function(f){
f();
});
//
//
...
//
The output will be 10 all the time.
If we swtich var to let:
var fs = [];
for(let i = 0; i < 10; i++){
fs.push(function(i){
console.log(i)
});
}
fs.forEach(function(f){
f();
});
//
//
...
//
Then the output is 0-9. The reason for that is because, let keyword each time it create a new instance in for loop.
What this really means in the end is that if you're used to bringing your variables up to the top of a scope using VAR and things like VAR i, VAR temp, where you want to be careful, because you're afraid of wasting behaviors due to this i.
function varFun(){
var previous = 0;
var current = 1;
var i;
var temp;
for(i = 0; i < 10; i++){
temp = previous;
previous = current;
current = temp + current;
}
}
function letFun(){
let previous = 0;
let current = 1;
for(let i = 0; i < 10; i++){
let temp = previous;
previous= current;
current = temp + current;
}
}
Feel free now to use the LET keyword, and instead of declaring it at the top, you can declare it in line, inside of the FOR statement, as well as declaring it inside of the FOR block, and it'll safely create this temp each time it goes through the FOR block.
Using let instead of var prevents variable declarations from being moved to the top of the scope on what is known as hositing.
[ES6] 03. The let keyword -- 1的更多相关文章
- [ES6] 05. The leg keyword -- 3. Block Scope
In ES6, IIFE is not necessary: // IIFE写法 (function () { var tmp = ...; ... }()); // 块级作用域写法 { let tm ...
- [ES6] 04. The let keyword -- 2 Fiald case
Fiald case 1: let can work in it's block { let a = 10; var b = 1; } a // ReferenceError: a is not de ...
- ES6 Syntax and Feature Overview
View on GitHub Note: A commonly accepted practice is to use const except in cases of loops and reass ...
- ES6中的数组
数组是js中很重要的数据类型,虽然在 ES5 中,关于数组的方法和属性很多.但为了更加简洁.高效的操作数组,ES6 中又在数组原型上和实例上新增了一些方法. 一.Array方法 1.1 Array.f ...
- [Javascript] ES6 Class Constructors and the Super Keyword
When the ES6 class shipped back in 2015, a number of additional keywords came with it. Two of these ...
- 【JS复习笔记】03 继承(从ES5到ES6)
前言 很久以前学习<Javascript语言精粹>时,写过一个关于js的系列学习笔记. 最近又跟别人讲什么原型和继承什么的,发现这些记忆有些模糊了,然后回头看自己这篇文章,觉得几年前的学习 ...
- [ES6系列-03]ES6中关于参数相关特性详解(参数默认值与参数解构赋值与剩余参数)
[原创] 码路工人 大家好,这里是码路工人有力量,我是码路工人,你们是力量. 今天总结一下 ES6 中跟参数相关的内容. 欢迎补充斧正.留言交流. 让我们互相学习一起进步. 1. ES6 参数默认值( ...
- 03 | 变量的解构赋值 | es6
变量的解构赋值 数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring). 以前,为变量赋值,只能直接指定值. let a ...
- [Javascript] Understand common misconceptions about ES6's const keyword
Values assigned with let and const are seen everywhere in JavaScript. It's become common to hear the ...
随机推荐
- POJ3292 Semi-prime H-numbers [数论,素数筛]
题目传送门 Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10871 Acce ...
- C51中的xdata
今天在写公司芯片对应的.H文件时,在定义寄存器的时候出现一个问题. 我们芯片的寄存器地址是定义在片外RAM区的,不能使用sfr这样的关键字来进行定义. 后来查阅了对应的资料才发现定义寄存器有很多种方法 ...
- Kail Linux渗透测试教程之Recon-NG框架
Kail Linux渗透测试教程之Recon-NG框架 信息收集 信息收集是网络攻击最重要的阶段之一.要想进行渗透攻击,就需要收集目标的各类信息.收集到的信息越多,攻击成功的概率也就越大.本章将介绍信 ...
- Hat's Fibonacci hdu 1250
Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...
- Hibernate 使用MyEclipse简化开发
在平时开发中写配置文件比较繁琐,在这里写一下如何使用myEclipse简化开发. 1.打开MyEclipse,创建数据库连接 单机测试连接按钮,如果出现成功建立连接,则连接成功. 然后Finish 2 ...
- 【UOJ #112】【APIO 2015】Palembang Bridges
http://uoj.ac/problem/112 先扣掉在同一侧的情况. 当\(k=1\)时,桥建在所有位置的中位数. 当\(k=2\)时,对于每个居民\((S_i,T_i)\),这个居民只会走离\ ...
- 【暴力】Gym - 100851A - Adjustment Office
题意:给你一个n*n的矩阵,初始时,(x,y)的值为x+y.可能有两类操作,一类是对某一行求和,并将这一行置零:另一类是对某一列求和,并将这一列置零. 维护四个值:一个是列标号之和,一个是当前存在的列 ...
- 求矩阵中各列数字的和 Exercise08_01
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:求矩阵中各列数字的和 * */ public class Exercise ...
- web.xml2.3配置需要注意的顺序问题
今天在做一个街道办事处项目时,用了eWebeditor编辑器,在按照说明文件进行web.xml配置时,在<web-app>一行上出现红叉,提示信息为:The content of elem ...
- Intelij Idea下的git使用
一.简介 在我们入门软件研发并且是团队开发的时候,总会遇到这样合代码这么简单粗暴的工作,最开始我也能体会到这项工作折磨.那git是干什么得呢?简称分布式版本控制系统,常见就是上传代码.整合代码.更新代 ...