https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable-in-jav

答案1

The difference is scoping. var is scoped to the nearest function block and let is scoped to the nearest enclosing block, which can be smaller than a function block. Both are global if outside any block.

Also, variables declared with let are not accessible before they are declared in their enclosing block. As seen in the demo, this will throw a ReferenceError exception.

var html = '';

write('#### global ####\n');
write('globalVar: ' + globalVar); //undefined, but visible try {
write('globalLet: ' + globalLet); //undefined, *not* visible
} catch (exception) {
write('globalLet: exception');
} write('\nset variables'); var globalVar = 'globalVar';
let globalLet = 'globalLet'; write('\nglobalVar: ' + globalVar);
write('globalLet: ' + globalLet); function functionScoped() {
write('\n#### function ####');
write('\nfunctionVar: ' + functionVar); //undefined, but visible try {
write('functionLet: ' + functionLet); //undefined, *not* visible
} catch (exception) {
write('functionLet: exception');
} write('\nset variables'); var functionVar = 'functionVar';
let functionLet = 'functionLet'; write('\nfunctionVar: ' + functionVar);
write('functionLet: ' + functionLet);
} function blockScoped() {
write('\n#### block ####');
write('\nblockVar: ' + blockVar); //undefined, but visible try {
write('blockLet: ' + blockLet); //undefined, *not* visible
} catch (exception) {
write('blockLet: exception');
} for (var blockVar = 'blockVar', blockIndex = 0; blockIndex < 1; blockIndex++) {
write('\nblockVar: ' + blockVar); // visible here and whole function
}; for (let blockLet = 'blockLet', letIndex = 0; letIndex < 1; letIndex++) {
write('blockLet: ' + blockLet); // visible only here
}; write('\nblockVar: ' + blockVar); try {
write('blockLet: ' + blockLet); //undefined, *not* visible
} catch (exception) {
write('blockLet: exception');
}
} function write(line) {
html += (line ? line : '') + '<br />';
} functionScoped();
blockScoped(); document.getElementById('results').innerHTML = html;

Global:

They are very similar when used like this outside a function block.

let me = 'go';  // globally scoped
var i = 'able'; // globally scoped

However, global variables defined with let will not be added as properties on the global windowobject like those defined with var.

console.log(window.me); // undefined
console.log(window.i); // 'able'

Function:

They are identical when used like this in a function block.

function ingWithinEstablishedParameters() {
let terOfRecommendation = 'awesome worker!'; //function block scoped
var sityCheerleading = 'go!'; //function block scoped
}

Block:

Here is the difference. let is only visible in the for() loop and var is visible to the whole function.

unction allyIlliterate() {
//tuce is *not* visible out here for( let tuce = 0; tuce < 5; tuce++ ) {
//tuce is only visible in here (and in the for() parentheses)
//and there is a separate tuce variable for each iteration of the loop
} //tuce is *not* visible out here
} function byE40() {
//nish *is* visible out here for( var nish = 0; nish < 5; nish++ ) {
//nish is visible to the whole function
} //nish *is* visible out here
}

Redeclaration:

Assuming strict mode, var will let you re-declare the same variable in the same scope. On the other hand, let will not:

'use strict';
let me = 'foo';
let me = 'bar'; // SyntaxError: Identifier 'me' has already been declared
'use strict';
var me = 'foo';
var me = 'bar'; // No problem, `me` is replaced.

答案2

let can also be used to avoid problems with closures. It binds fresh value rather than keeping an old reference as shown in examples below.

for(var i = 1; i < 6; i++) {
document.getElementById('my-element' + i)
.addEventListener('click', function() { alert(i) })
}

Code above demonstrates a classic JavaScript closure problem. Reference to the i variable is being stored in the click handler closure, rather than the actual value of i.

Every single click handler will refer to the same object because there’s only one counter object which holds 6 so you get six on each click.

General workaround is to wrap this in an anonymous function and pass i as argument. Such issues can also be avoided now by using let instead var as shown in code below.

'use strict';

for(let i = 1; i < 6; i++) {
document.getElementById('my-element' + i)
.addEventListener('click', function() { alert(i) })
}

What's the difference between using “let” and “var” to declare a variable in JavaScript?的更多相关文章

  1. Javascript——概述 && 继承 && 复用 && 私有成员 && 构造函数

    原文链接:A re-introduction to JavaScript (JS tutorial) Why a re-introduction? Because JavaScript is noto ...

  2. JavaScript var, let, const difference All In One

    JavaScript var, let, const difference All In One js var, let, const 区别 All In One 是否存在 hoisting var ...

  3. 【译】PHP的变量实现(给PHP开发者的PHP源码-第三部分)

    文章来自:http://www.aintnot.com/2016/02/12/phps-source-code-for-php-developers-part3-variables-ch 原文:htt ...

  4. Expression Tree Basics 表达式树原理

    variable point to code variable expression tree data structure lamda expression anonymous function 原 ...

  5. Groovy 模版引擎

    1. Introduction Groovy supports multiple ways to generate text dynamically including GStrings, print ...

  6. js 日期按年月日加减

    <script> function isleapyear(year) { if(parseInt(year)%4==0 && parseInt(year)%100!=0)r ...

  7. coffeescript 1.8.0 documents

    CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque ...

  8. JavaScript闭包的底层运行机制

    转自:http://blog.leapoahead.com/2015/09/15/js-closure/ 我研究JavaScript闭包(closure)已经有一段时间了.我之前只是学会了如何使用它们 ...

  9. SAS Annotated Output GLM

    SAS Annotated Output GLM   在使用SAS过程中,proc glm步输出离差平方和有4种算法,分别是SS1 SS2 SS3 SS4 下面文章介绍了其中SS3的具体计算步骤和例子 ...

随机推荐

  1. Unity3d 多次显示关闭一个UI

    publicclass OpenClooseGoUI : MonoBehaviour { public  GameObject   closeBt; public  GameObject   goUI ...

  2. PowerDesigner之设置(1)

    这里用到的数据库为SQL2000. PowerDesigner 16. 原始格式: (个人认为的缺点) 创建表前先将原来的表删除(创建表逻辑,假如存在则drop.假如原来有数据,用drop就有问题.新 ...

  3. linux解压war包

    可以用unzip命令 unzip project.war -d project 这样就在当前目录下解压project.war到project目录里面,参数-d的意思是创建project目录 附:unz ...

  4. Grafana---graph

    主面板简单的命名为Graph.它提供了一组非常丰富的图形选项. 单击面板的标题将显示一个菜单.edit选项为面板打开了额外的配置选项. 一.General general允许定制面板的外观和菜单选项. ...

  5. Jquery Ajax Json ashx 实现前后台数据传输

    经过一个多星期的研究,各种查找资料终于自己实现了Jquery  Ajax Json ashx 的前后台数据交流功能 首先一点,Ajax只能对应一个ashx文件,多余两个,如果打开异步传输的async: ...

  6. qs.parse()、qs.stringify()、JSON.stringify() 用法及区别

    在处理数据的时候,有时候我们需要将对象和字符串和json之间进行转换,这个时候我们可以使用以下的方法 qs是一个npm仓库所管理的包,可通过npm install qs命令进行安装. qs.strin ...

  7. 洛谷 P2523 [HAOI2011]Problem c

    洛谷1或洛谷2,它们是一样的题目,手动滑稽- 这一题我是想不出来, 但是我想吐槽一下坐我左边的大佬. 大佬做题的时候,只是想了几分钟,拍了拍大腿,干脆的道:"这不是很显然吗!" 然 ...

  8. hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Ot ...

  9. (4.4)dbcc checkdb 数据页修复

    转自:http://blog.51cto.com/lzf328/955852 三篇 一.创建错误数据库 以前看Pual写过很多数据恢复的文章,他很多的测试都是自己创建的Corrupt数据库,其实我们自 ...

  10. Python中使用MySQL

    @1: 安装MySQL: lxw ~$ sudo apt-get install mysql-server 可以通过 sudo netstat -tap | grep mysql` 命令检查之后,如果 ...