[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 ...
随机推荐
- 详细理解Java虚拟机的运行过程
基本概述: Java虚拟机简称JVM,是JRE中的一部分,也是Java程序运行的最关键的部分.完整的Java运行流程大致包括编译.java文件形成.class文件,然后根据.class文件的内容进行一 ...
- 小程序登陆遇到 ERR_REQUEST_PARAM
小程序测试环境突然登陆不上,返回的错误信息是{}"code":-1,"error":"ERR_REQUEST_PARAM"}. 小程序登陆代 ...
- Unity 2D游戏开发教程之精灵的死亡和重生
Unity 2D游戏开发教程之精灵的死亡和重生 精灵的死亡和重生 目前为止,游戏项目里的精灵只有Idle和Walking这两种状态.也就是说,无论精灵在游戏里做什么,它都不会进入其它的状态,如死亡.于 ...
- 【BZOJ 4503】4503: 两个串 (FFT)
4503: 两个串 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 497 Solved: 226 Description 兔子们在玩两个串的游戏.给 ...
- 比较IBM MQSeries和BEA WebLogic JMS Server(转载)
在面向消息的中间件(MOM)这个领域,IBM MQSeries (又称WebSphere MQ)一直是当仁不让的超级大哥,其它还有一些小兄弟,比如SwiftMQ.SonicMQ之类.但近年来随着J2E ...
- zend studio10 创建重复project from remote server
zend studio10创建重复project from remote server [本地远程到dev服务器]1.删掉已存在project隐藏文件.project .build2.如果zend10 ...
- poj 3261 求可重叠的k次最长重复子串
题意:求可重叠的k次最长重复子串的长度 链接:点我 和poj1743差不多 #include<cstdio> #include<iostream> #include<al ...
- java高并发程序设计模式-并发级别:阻塞、无障碍、无锁、无等待【转载】
一般认为并发可以分为阻塞与非阻塞,对于非阻塞可以进一步细分为无障碍.无锁.无等待,下面就对这几个并发级别,作一些简单的介绍. 1.阻塞 阻塞是指一个线程进入临界区后,其它线程就必须在临界区外等待,待进 ...
- wampserver -- 取消PHP页面Warning和Notice级别的报错
Learn from:http://yige.org/p/91 一般遇到这样的问题,有两个方法:1.如果有服务器权限,直接把服务器上的php.ini的配置改了,改成不输出Warning和Notice级 ...
- [转]安卓虚拟机启动后报错: 类似 SDK Manager] Error: Error parsing .devices.xml 解决方案
昨天用android sdk manager 更新了android sdk, 我是在myeclipse上面安装adt来开发android的现在每次打开myeclipse都报错, 而且我每次打开虚拟机的 ...