ES6 / Es2015 introduces a lot of new extensions to the JavaScript world. One of these extensions are two new keywords used to variable declarations. So now in addition to the old
Suddenly you start writing code, you need to declare a variable and … there are questions like: when I use
First, we got acquainted closer with the
https://davidwalsh.name/for-and-against-let
Discussions on how to declare variable led my team to a discussion at the workshop within the company. Effect of such a summary here…
My small advice, learn/read about new stuff to get better understanding and do whats best fit to you and your team and stick to those choices…
var
keyworld we have got let
and const
that allows us to declare a variable with a block scope. Oh great it may seem, we have so much choice, but the choice becomes problematic when it comes to writing code …Suddenly you start writing code, you need to declare a variable and … there are questions like: when I use
const
, and when let
, can I still use the var
? These types of questions began to appear also in my team when we started to write in Node.js with ES6. It was important for us because we want in a team cohesive approach to the declaration, and because each member had a little different view of this, we had to discuss it …First, we got acquainted closer with the
let
and const
. about which many people have already written nice articles. I pasted below some interesting links to this topics.https://davidwalsh.name/for-and-against-let
David Walsh Blog.
For and against let
Examining JavaScript ES6’s new ‘let’ keyword block for scoping, and conditions where it should or should not be used.
http://blog.getify.com/constantly-confusing-const/Tearing down the confusions around the ES6 ‘const’ keywordhttps://ponyfoo.com/articles/es6-let-const-and-temporal-dead-zone-in-depth
Pony Foo. Let ES6, Const and the “Temporal Dead Zone” (TDZ) in Depth This is yet another edition of ES6 in Depth. First time here? Welcome! So far in the covered destructuring, template literals,> arrow functions, the spread operator and rest …and the last https://hacks.mozilla.org/2015/07/es6-in-depth-let-and-const/
Discussions on how to declare variable led my team to a discussion at the workshop within the company. Effect of such a summary here…
Steps to follow when declaring variable:
- If the variable is used throughout the function, use
const
or if a variable need reference rassigned , usevar
- If you need a variable with block sopce (for example, in if condition), use
const
, if variable need to be assigned to a another object, uselet
- use
let
withfor (;;)
loops.
const
, becouse thanks to this we secure ourself from accidently reasigned veriable.You need to remember that With const
you only receive unchenged reference to object but you can change objects properties. Objects declared with const
are not immutable unless you use Object.freeze()
(of course with nested object you shoul use Object.frezee()
with recursion).Quick summary
Is it realy worth it to follow some trends, fashion, not understanding why you should use this approach over another, or maybe it just because it looks prettier in the code? Why I should not usevar
if I know that my variable which I declared on top of the function will be used all over the function body…My small advice, learn/read about new stuff to get better understanding and do whats best fit to you and your team and stick to those choices…
Brak komentarzy:
Prześlij komentarz