ES6

17 Jan 2018 | | javascript

references and credits(Nicholas C. Zakas): Understanding ECMAScript 6

You’re a blockhead charlie brown!

As I read thru the ES6 specs I see so many features that the ruby language already has or does

ES6 updates

Block Bindings

In most C-based languages, variables (or bindings) are created at the spot where the declaration occurs. In JavaScript, however, this is not the case. Where your variables are actually created depends on how you declare them, and ECMAScript 6 offers options to make controlling scope easier.

Summary

The let and const block bindings introduce lexical scoping to JavaScript. These declarations are not hoisted and only exist within the block in which they are declared. This offers behavior that is more like other languages and less likely to cause unintentional errors, as variables can now be declared exactly where they are needed. As a side effect, you cannot access variables before they are declared, even with safe operators such as typeof. Attempting to access a block binding before its declaration results in an error due to the binding’s presence in the temporal dead zone (TDZ). “A variable declared with either let or const cannot be accessed until after the declaration.”

Block-level declarations are those that declare variables that are inaccessible outside of a given block scope. Block scopes, also called lexical scopes, are created:

  1. Inside of a function
  2. Inside of a block (indicated by the { and } characters)

In many cases, let and const behave in a manner similar to var; however, this is not true for loops. For both let and const, for-in and for-of loops create a new binding with each iteration through the loop. That means functions created inside the loop body can access the loop bindings values as they are during the current iteration, rather than as they were after the loop’s final iteration (the behavior with var). The same is true for let declarations in for loops, while attempting to use const declarations in a for loop may result in an error.

The current best practice for block bindings is to use const by default and only use let when you know a variable’s value needs to change. This ensures a basic level of immutability in code that can help prevent certain types of errors.

Example

If you want a variable to exist in the below code use the let outside of the if block.

     function getValue(condition) {
     
         if (condition) {
             let value = "blue";
     
             // other code
     
             return value;
         } else {
     
             // value doesn't exist here 
             // before ES6 the variable does exist here due to hoisting, but is undefined.
     
             return null;
         }
     
         // value doesn't exist here
         //  before ES6 the variable does exist but is undefined.
     }
   
   // let gives you scoping within a LOOP.
   // Consider this GOOD use:
   for (let i = 0; i < 10; i++) {
       process(items[i]);
   }
   
   // i is not accessible here - throws an error
   console.log(i);
comments powered by Disqus(comments off)

Older · View Archive (32)

react-ion

Reaction to React

Like everyone else, I see that React is taking the world by storm or so it would seem. I heard a Ruby Rogues podcast by DHH and he said:

“Still not a big fan on the framework side of things…React’s approach is sound but pairing up with redux..well is more convoluted and self-congratulatory in its complexity than anything I have seen since the dark ages of J2EE…and we have spent two decades on separation of concerns, and now react just says…Hey..lets put it all in the same box….that said…I found that a lot of the problems the new javascript technologies propose to solve are not problems that I have…”

I think I am going to wait till the hype dies down when and if I see a practical application. Not saying its bad or good, only that maybe its too much sugar for a dime as they say. Time will tell.

Newer

javascript concurrency

“Captain’s log stardate… unknown. During an ion storm the landing party has beamed back to the Enterprise and found it and the personnel aboard changed. The ship is subtly altered physically. Behavior and discipline has become brutal, savage.” Startrek Mirror Mirror episode parallel universe.

Javascript does not run things in parallel

Concurrency

“Concurrency can be defined as multiple things happening in the same time frame..but not really at the same time.