Thursday 7 February 2019

typescript - When should I use var or let in Angular 2?




I understand the differences in scope between 'var' and 'let' declarations and understand why 'let' can be more preferable as suggested by the respective docs, but why do so many examples in angular and typescript docs still frequently use var? It's hard to see a pattern in why one or another appears in examples.


Answer



Two reasons pop to mind. The first is that TypeScript's authors want you to know that there isn't always need for block scoping of a variable, in which case there's nothing wrong with var per se, as long as it is treated in the TypeScript way - such as by using declare - as seen in the documentation below.




https://www.typescriptlang.org/docs/handbook/declaration-files/by-example.html



The other case, at least in TypeScript's documentation, is when var pops up to document gotchas and other quirks in JavaScript that TypeScript can help iron out, as well as to show how TypeScript coexists with conventional JavaScript, as seen in the documentation below.



https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...