使用“let”和“var”在JavaScript中声明变量有什么区别?

前端之家收集整理的这篇文章主要介绍了使用“let”和“var”在JavaScript中声明变量有什么区别?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
ECMAScript 6引入了 the let statement.我听说它被描述为“本地”变量,但我仍然不太确定它与var关键字的行为有何不同.

有什么区别?何时应该使用var?

解决方法

区别在于范围界定. var的范围限定为最近的功能块,并且将范围限定为最近的封闭块,该封闭块可以小于功能块.如果在任何区域之外,两者都是全局

此外,使用let声明的变量在它们的封闭块中声明之前是不可访问的.如演示中所示,这将引发ReferenceError异常.

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('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;
<pre id="results"></pre>

全球:

功能块之外使用它们非常相似.

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

但是,使用let定义的全局变量不会作为属性添加到全局窗口对象上,就像使用var定义的那样.

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

功能

功能块中使用时它们是相同的.

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

块:

这是区别. let仅在for()循环中可见,var对整个函数可见.

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
}

重声明:

假设有严格模式,var将允许您在同一范围内重新声明相同的变量.另一方面,我们不会:

'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.
原文链接:https://www.f2er.com/js/157285.html

猜你在找的JavaScript相关文章