ES6中var的用例是什么? [英] What is the use case for var in ES6?
问题描述
如果 let
关键字引入了块范围的正确实现, var
是否再有一个用例?我正在从软件设计的角度来看待这个,而不是一个句法,你可以的立场。
如果
let
关键字引入了块范围的正确实现,var
再有一个用例?
可能有一个用例: let
全局范围内的声明在全局对象上创建一个属性。示例:
use strict; // for chromevar foo = 42; let bar = 21; console.log('window.foo(var)',window.foo); // 42console.log('window.bar(let)',window.bar); //未定义
对象环境记录全局环境记录的组件包含所有内置全局变量的绑定(子句18 )以及由 FunctionDeclaration , GeneratorDeclaration 或引入的所有绑定> VariableStatement 包含在全局代码中。全局代码中所有其他ECMAScript声明的绑定都包含在全局环境记录的声明性环境记录组件中。
然而,这个也可以通过直接分配给全局对象来创建一个显式全局变量来解决这个问题:
window.foo = 42;
这也是创建全局类btw的唯一方法,因为类
声明具有相同的行为。
(注意:我不主张使用全局变量)
有一些语法结构,您只能使用 var
,但这更多的是规范如何演变,实际上并不实际。例如:
if(true)
var foo = 42; //有效但有种无用或坏设计
// vs
if(true)
let foo = 42; //无效
块范围不是唯一有用的功能虽然时间死区 是另一个方便的功能,更容易找到错误。比较:
var foo = 42;
function bar(){
console.log(foo); // undefined
var foo = 21;
}
bar();
// vs
var foo = 42; //或`let`,没关系
function bar(){
console.log(foo); // ReferenceError,temporal dead zone
let foo = 21;
}
bar();
尝试访问 let $ c时,您会收到参考错误$ c>尚未初始化的变量。
If the let
keyword introduces a proper implementation of block scope, does var
any longer have a use case? I am looking at this from a software design standpoint rather than a syntactical, "well you could" standpoint.
If the
let
keyword introduces a proper implementation of block scope, doesvar
any longer have a use case?
There could be one use case: let
declarations in global scope don't create a property on the global object. Example:
"use strict"; // for chrome
var foo = 42;
let bar = 21;
console.log('window.foo (var)', window.foo); // 42
console.log('window.bar (let)', window.bar); // undefined
From 8.1.1.4 Global Environment Records
The object Environment Record component of a global Environment Record contains the bindings for all built-in globals (clause 18) and all bindings introduced by a FunctionDeclaration, GeneratorDeclaration, or VariableStatement contained in global code. The bindings for all other ECMAScript declarations in global code are contained in the declarative Environment Record component of the global Environment Record.
However, this can also easily be solved by creating an explicit global variable using by assigning to the global object directly:
window.foo = 42;
This would also be the only way to create global classes btw, because the class
declaration has the same behavior.
(Note: I'm not advocating the use of global variables)
There are syntax constructs where you can only use var
, but that's more a consequence of the how the spec evolved and doesn't really serve any practical purpose. For example:
if (true)
var foo = 42; // valid but kind of useless or bad design
// vs
if (true)
let foo = 42; // invalid
Block scope is not the only useful feature though. The temporal dead zone is another handy feature to find bugs more easily. Compare:
var foo = 42;
function bar() {
console.log(foo); // undefined
var foo = 21;
}
bar();
// vs
var foo = 42; // or `let`, doesn't matter
function bar() {
console.log(foo); // ReferenceError, temporal dead zone
let foo = 21;
}
bar();
You get a reference error when trying to access a let
variable that wasn't initialized yet.
这篇关于ES6中var的用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!