ECMAScript 6介绍了该let声明.我听说它被描述为"本地"变量,但我仍然不太确定它与var关键字的行为有何不同.
有什么区别?何时应该let使用var?
我已经玩ES6一段时间了,我注意到虽然声明的变量var按预期提升了......
console.log(typeof name); // undefined
var name = "John";
Run Code Online (Sandbox Code Playgroud)
... 用吊装声明let或const似乎有一些问题的变量:
console.log(typeof name); // ReferenceError
let name = "John";
Run Code Online (Sandbox Code Playgroud)
和
console.log(typeof name); // ReferenceError
const name = "John";
Run Code Online (Sandbox Code Playgroud)
这是否意味着变量声明let或未声明const?这是怎么回事?这个问题let和const这个问题有什么区别吗?
这是一系列关于JavaScript中语法的问题.这也是社区Wiki,因此邀请每个人参与维护此列表.
Stack Overflow不允许搜索特定字符.因此,在搜索运算符和其他语法标记时,很难找到许多关于运算符和其他语法标记的问题.这也使得关闭重复更加困难.以下列表是为了解决此问题.
主要思想是在Stack Overflow上链接现有问题,因此我们更容易引用它们,而不是复制ECMAScript规范中的内容.
此外,这是PHP符号引用的公然副本.我们需要一个JS.
请帮忙.编辑并添加指向其他运算符/语法参考的链接,或者如果您无法在特定语法上找到好的问题/答案,请添加此问题的答案并将其链接
在本教程中有写:
If you redeclare a JavaScript variable, it will not lose its value.
我为什么要重新声明变量?在某些情况下它是否实用?
谢谢
在node.js v6.0.0中
function testlet() {
let a = 0;
for (var i = 0; i < 100000000; i++) {}
}
function testlet2() {
for (var i = 0; i < 100000000; i++) {}
let a = 0;
}
console.time('let');
testlet();
console.timeEnd('let');
console.time('let2');
testlet2();
console.timeEnd('let2'); Run Code Online (Sandbox Code Playgroud)
如何let在代码中的位置导致如此大的性能差异?
谁能解释我为什么创建它?
它引发ReferenceError或SyntaxError而不是返回undefined的逻辑是什么?
参考错误:
console.log(typeof foo);
let foo;Run Code Online (Sandbox Code Playgroud)
语法错误:
let foo;
let foo;Run Code Online (Sandbox Code Playgroud)
不明确的:
console.log(typeof foo);Run Code Online (Sandbox Code Playgroud)
class App {
constructor() {
this.canvas = document.createElement('canvas');
document.body.appendChild(this.canvas);
this.ctx = this.canvas.getContext('2d');
this.pixelRatio = window.devicePixelRatio > 1 ? 2 : 1;
window.addEventListener('resize', this.resize.bind(this), false);
this.resize();
window.requestAnimationFrame(this.animate);
}
resize() {
this.stageWidth = document.body.clientWidth;
this.stageHeight = document.body.clientHeight;
}
animate = () => {
this.test(); // ---> here!
};
test = () => {
console.log('here!');
};
}
window.onload = () => {
new App();
};Run Code Online (Sandbox Code Playgroud)
箭头函数不会被提升,只有常规函数会被提升。为什么animate函数内部可以调用this.test?类中箭头函数的不同行为?
我试图理解打字稿中的提升。提升是否在 Typescript 中进行,如果是,与 Javascript 中的提升方式相比有什么不同吗?
例如:即使我在使用它的函数之后声明了接口,代码也可以很好地转换。可以安全地假设它的发生是由于作为转译的一部分的提升而发生的,或者这里还涉及其他东西。
getCarDetails({name: 'Xyz', topSpeed: 300})
function getCarDetails(carDetails: CarDetails) {
console.log(carDetails.name);
console.log(carDetails.topSpeed);
}
interface CarDetails {
name: string;
topSpeed: number;
}
Run Code Online (Sandbox Code Playgroud) function foo(a,b){
return a + b;
}
foo(1,2);Run Code Online (Sandbox Code Playgroud)
函数参数是否提升?
函数执行上下文创建阶段的变量环境是否看起来像这样:
VE = {
{ 0 : undefined , 1: undefined, length: 2 },
{a : undefined, b: undefined},
outer: refToGlobalLE
}
Run Code Online (Sandbox Code Playgroud) javascript ×10
hoisting ×3
let ×3
ecmascript-6 ×2
const ×1
declaration ×1
node.js ×1
scope ×1
typescript ×1
var ×1
variables ×1