使用“let”之间有什么区别?和“var”声明一个变量? [英] What's the difference between using "let" and "var" to declare a variable?
问题描述
ECMAScript 6引入了 让
语句。我听说它是一个本地变量,但我仍然不太清楚它的行为与 var
关键字的不同。
有什么区别? 允许
在 var
中使用
区别在于范围界定。 var
的范围限定于最近的功能块, let
的范围限定为最近的封闭块,它可以小于功能块。两者都是全局的,如果在任何块之外。
另外,使用 let
声明的变量在被声明之前是不可访问的他们的封闭块。如演示中所示,这将引发一个ReferenceError异常。
演示:
var html =''; write('#### global #### \\\
'); write('globalVar:'+ globalVar); // undefined,but visibletry {write('globalLet:'+ globalLet); // undefined,* not * visible} catch(exception){write('globalLet:exception');} write('\\\
set variables'); var globalVar ='globalVar'; let globalLet ='globalLet'; write '\\\
globalVar:'+ globalVar); write('globalLet:'+ globalLet); function functionScoped(){write('\\\
#### function ####'); write('\\\
functionVar:'+ functionVar); // undefined,但可见try {write('functionLet:'+ functionLet); // undefined,* not * visible} catch(exception){write('functionLet:exception'); } write('\\\
set variables'); var functionVar ='functionVar';让functionLet ='functionLet'; write('\\\
functionVar:'+ functionVar); write('functionLet:'+ functionLet);} function blockScoped(){write('\\\
#### block ####'); write('\\\
blockVar:'+ blockVar); // undefined,但可见try {write('blockLet:'+ blockLet); // undefined,* not * visible} catch(exception){write('functionLet:exception'); } for(var blockVar ='blockVar',blockIndex = 0; blockIndex< 1; blockIndex ++){write('\\\
blockVar:'+ blockVar); // visible here and whole function}; for(let blockLet ='blockLet',letIndex = 0; letIndex< 1; letIndex ++){write('blockLet:'+ blockLet); // visible only here}; write('\\\
blockVar:'+ blockVar);尝试{write('blockLet:'+ blockLet); // undefined,* not * visible} catch(exception){write('functionLet:exception'); }} function write(line){html + =(line?line:'')+'< br />';}functionScoped();blockScoped();document.getElementById('results').innerHTML = html ;
< pre id =results>< ; / pre>
全局:
它们在函数块外使用时非常相似。
让我='去' // global scoped
var i ='able'; //全局范围
然而,使用定义的全局变量let
不会作为属性添加到全局窗口
对象,如用 var
定义的对象。
console.log(window.me); // undefined
console.log(window.i); //'能'
功能:
它们在函数块中使用时相同。
function ingWithinEstablishedParameters(){
let terOfRecommendation ='awesome worker!'; // function block scoped
var sityCheerleading ='go!'; //功能块范围
}
阻止:
这里有区别。 let
仅在()循环的中可见,
var
对于整个函数是可见的。
function allyIlliterate(){
/ pre>
// tuce is * not * visible out here
for(let tuce = 0; tuce< 5; tuce ++){
// tuce只在这里可见(和for()括号)
//并且循环的每次迭代都有一个单独的tuce变量
}
// tuce is * not * visible out here
}
函数byE40(){
// nish * is * visible out here
for(var nish = 0; nish< 5; nish ++){
// nish可以看到整个函数
}
// nish *是*可见在这里
}
重新声明:
假设严格模式,
var
将允许您在同一范围内重新声明同一个变量。另一方面,let
不会:'use strict';
让我='foo';
让我='bar'; // SyntaxError:标识符'我'已经被声明为
'use strict';
var me ='foo';
var me ='bar'; //没问题,我被替换。
ECMAScript 6 introduced the
let
statement. I've heard it described as a "local" variable, but I'm still not quite sure how it behaves differently than thevar
keyword.What are the differences? When should
let
be used overvar
?解决方案The difference is scoping.
var
is scoped to the nearest function block andlet
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.Demo:
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('functionLet: 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('functionLet: exception'); } } function write(line) { html += (line ? line : '') + '<br />'; } functionScoped(); blockScoped(); document.getElementById('results').innerHTML = html;
<pre id="results"></pre>
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 globalwindow
object like those defined withvar
.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 thefor()
loop andvar
is visible to the whole function.function 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.
这篇关于使用“let”之间有什么区别?和“var”声明一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!