ECMAScript 6 引入了许可声明。
我听说它被描述为一个当地变量,但我仍然不确定它是如何行为不同于 var 关键词。
什么是差异?什么时候应该被允许使用而不是 var?
ECMAScript 6 引入了许可声明。
我听说它被描述为一个当地变量,但我仍然不确定它是如何行为不同于 var 关键词。
什么是差异?什么时候应该被允许使用而不是 var?
当前回答
// An array of adder functions.
var adderFunctions = [];
for (var i = 0; i < 1000; i++) {
// We want the function at index i to add the index to its argument.
adderFunctions[i] = function(x) {
// What is i bound to here?
return x + i;
};
}
var add12 = adderFunctions[12];
// Uh oh. The function is bound to i in the outer scope, which is currently 1000.
console.log(add12(8) === 20); // => false
console.log(add12(8) === 1008); // => true
console.log(i); // => 1000
// It gets worse.
i = -8;
console.log(add12(8) === 0); // => true
上面的过程不会产生所需的函数序列,因为我的范围超越了每个函数创建的区块的 iteration。 相反,在环节结束时,每个函数的 i 关闭指在环节结束时的 i 值(1000)为每个在 adder 中的匿名函数。
// Let's try this again.
// NOTE: We're using another ES6 keyword, const, for values that won't
// be reassigned. const and let have similar scoping behavior.
const adderFunctions = [];
for (let i = 0; i < 1000; i++) {
// NOTE: We're using the newer arrow function syntax this time, but
// using the "function(x) { ..." syntax from the previous example
// here would not change the behavior shown.
adderFunctions[i] = x => x + i;
}
const add12 = adderFunctions[12];
// Yay! The behavior is as expected.
console.log(add12(8) === 20); // => true
// i's scope doesn't extend outside the for loop.
console.log(i); // => ReferenceError: i is not defined
每个函数现在保留在函数创建时的 i 的值,并且 adderFunctions 按照预期行事。
现在,图像将两种行为混合在一起,你可能会看到为什么不建议在同一脚本中混合更新的Let和 const。
const doubleAdderFunctions = [];
for (var i = 0; i < 1000; i++) {
const j = i;
doubleAdderFunctions[i] = x => x + i + j;
}
const add18 = doubleAdderFunctions[9];
const add24 = doubleAdderFunctions[12];
// It's not fun debugging situations like this, especially when the
// code is more complex than in this example.
console.log(add18(24) === 42); // => false
console.log(add24(18) === 42); // => false
console.log(add18(24) === add24(18)); // => false
console.log(add18(24) === 2018); // => false
console.log(add24(18) === 2018); // => false
console.log(add18(24) === 1033); // => true
console.log(add24(18) === 1030); // => true
不要让这件事发生在你身上,使用灯具。
其他回答
msg = “Hello World” 函数 doWork() { // msg 将是可用的,因为它被定义在这个开幕式上! 让朋友 = 0; console.log(msg); // 与 VAR 虽然: for (var iCount2 = 0; iCount2 < 5; iCount2++) {} // iCount2 将在这个开幕式后可用! console.log(iCount2); for (let iCount1 = 0; iCount1 < 5; iCount1++) {} // iCount1 将没有
下面是让关键词的解释,有几个例子。
主要区别在于,一个变量的范围是整个关闭功能。
此图在维基百科上显示哪些浏览器支持JavaScript 1.7.
请注意,只有 Mozilla 和 Chrome 浏览器支持它. IE、Safari 和其他可能不支持它。
下面表明“让我们”和“存在”在范围内是如何不同的:
let gfoo = 123;
if (true) {
let gfoo = 456;
}
console.log(gfoo); // 123
var hfoo = 123;
if (true) {
var hfoo = 456;
}
console.log(hfoo); // 456
gfoo,定义为 let 最初是在全球范围内,当我们宣布 gfoo 再次在如果条款的范围改变,当一个新的值被分配到变量在该范围内,它不会影响全球范围。
虽然 hfoo,由 var 定义,起初在全球范围内,但再次当我们在 if 条款内宣布它时,它考虑到全球范围 hfoo,尽管 var 已再次被用来宣布它。
有一些微妙的差异 - 让滑动行为更像变量滑动在更多或更少的任何其他语言。
例如,它转向封锁区块,它们在被宣布之前不存在,等等。
然而,值得注意的是,让它只是新的JavaScript实施的一部分,并且有不同的浏览器支持程度。
在2015年之前,使用 var 关键字是宣布 JavaScript 变量的唯一方式。
在 ES6 (JavaScript 版本) 之后,它允许 2 个新的关键字 let & const。
let = 可以重新分配 const = 不能重新分配 const ( const 源于常态,短形的“const” )
例子:
假设,宣布一个国家名称 / 你的母亲名称, const 是最合适的这里. 因为有更少的机会改变一个国家名称或你的母亲名称很快或晚些时候. 你的年龄,体重,工资,自行车的速度和更多类似于这些类型的数据,经常改变或需要重新分配。