简体   繁体   English

'let和'var'在打字稿中是一样的吗?

[英]'let and 'var' are the same in Typescript?

I was taking a look at AngularJS 2 and Typescript and I decided to make something with this just to learn the basics of Typescript. 我看了看AngularJS 2和Typescript,我决定为此做些事情,只是为了学习Typescript的基础。 With many research I found good topics about modules, Typescript, and one of them was talking about the 'let' and 'var' command to declare variables; 通过大量研究,我发现了有关模块,Typescript的好话题,其中之一是谈论“ let”和“ var”命令来声明变量。 according to this question, the Typescript code below should display only one alert and throw an error in the console: 根据问题,下面的Typescript代码应仅显示一个警报并在控制台中引发错误:

test.ts: test.ts:

for(let i = 0; i < 1; i++) {
    alert(i);
}
alert(i);

Compiled test.js: 编译的test.js:

for(var i = 0; i < 1; i++) {
    alert(i);
}
alert(i);
//# sourceMappingURL=test.js.map

But it isn't. 但事实并非如此。 The compiler "ignores" the "let" command and turns it into the "var" command. 编译器“忽略”“ let”命令,并将其转换为“ var”命令。 Why does this happen? 为什么会这样? Does Typescript only works properly with classes? Typescript是否仅适用于类?

I'm using AngularJS configuration for 'npm start', so it compiles my 'test.ts' file automatically: 我将AngularJS配置用于“ npm start”,因此它会自动编译我的“ test.ts”文件:

  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },

But it isn't. 但事实并非如此。 The compiler "ignores" the "let" command and turns it into the "var" command. 编译器“忽略”“ let”命令,并将其转换为“ var”命令。 Why does this happen? 为什么会这样? Does Typescript only works properly with classes? Typescript是否仅适用于类?

The compiler by default transpiles to ES3. 默认情况下,编译器会转换为ES3。 The let keyword doesn't exist in ES3 and so the emitter must emit code using syntax available in ES3... in this case the best replacement for the let keyword is the var keyword. let关键字在ES3中不存在,因此发射器必须使用ES3中可用的语法来发出代码...在这种情况下, let关键字的最佳替代方法是var关键字。

If you want it to emit with the let keyword, then you must target ES6— "target": "es6" in tsconfig.json or the command line option --target es6 . 如果你想让它与发射let关键字,则必须针对ES6- “ "target": "es6"tsconfig.json或命令行选项--target es6 Doing this will output with the same code that you inputted. 这样做将输出与您输入的代码相同的代码。

Note that even though your code works at runtime, it throws an error to let you know you have made a mistake at compile time: 请注意,即使您的代码在运行时运行,它也会引发错误,使您知道在编译时犯了一个错误:

for(let i = 0; i < 1; i++) {
    alert(i);
}
alert(i); // compile error: cannot find name 'i'

In this example, var and let have the same effect, with var being a little faster on most JS engines, so TypeScript does some performances optimization for you by changing that to a var . 在此示例中, varlet具有相同的效果,在大多数JS引擎上var快一点,因此TypeScript通过将其更改为var为您进行了一些性能优化。

Now if you try a different example, you will see that let isn't just changed into var , but more magic happens: 现在,如果尝试其他示例,您会发现let不仅变成了var ,而且发生了更多的魔术:

for (let i = 0; i < 3; i++) {
  setTimeout(function() { alert(i); });
}

Indeed in this example let and var wouldn't have the same effect. 实际上,在此示例中, letvar不会具有相同的效果。 let would display 1 2 3 while using var we would see 3 3 3 . let我们在使用var显示1 2 3 ,我们将看到3 3 3 If you want to learn more about the let keyword introduced by ES6 you can check this: 如果您想了解有关ES6引入的let关键字的更多信息,可以检查以下内容:

https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Instructions/let https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Instructions/let

They are identical but there is a difference when they used inside a function. 它们是相同的,但在函数内部使用时有所不同

LET

function theDifference(){
    for(let emre = 0; emre < 10; emre++){
    // emre is only visible inside of this for()
    }

// emre is NOT visible here.
}

VAR VAR

function theDifference(){
    for(var emre = 0; emre < 10; emre++){
    // emre is visible inside of this for()
    }

// emre is visible here too.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM