⬆️ ⬇️

Lock Visibility with Let in JavaScript

Let is such a keyword with which we can define a variable with a limited scope, and we can limit this scope in as many as four ways. People who know OCaml will immediately find a parallel between let ... in in Ocaml and let in JavaScript.

For enough words, let's look at every way we use let.







Enable Let support



Perhaps you will not be able to use Let at once, but do not worry, Let will work when you specify type = "application / javascript; version = 1.7" or call version (170).

')

Scope for one block



With the help of let, we can define a variable visible only in a specific block of code.

The syntax for this definition is:

let (define vars) {

some expression

}



And to make it clearer, let's consider a simple example. Let Bob live in Russia and want to buy a DreamPlug, he knows that in the CIS this thing will be worth $ 300, he goes to Europe to buy cheaper, buys and returns to Russia, and boasts to his friends that he has a computer from the outlet and when asked about he answers the price of $ 300.

See this example

//Bob   dreamplug = new Object(); dreamplug.price = 300; let(dreamplug.price = 150){ //Bob    Bob.buy(dreamplug); // Bob  dream plug  150$ } //Bob    Bob.onmessage = function(event){ if(event.data == " ?")Bob.postmessage(price + "$",event.origin) // Bob-    } //        Bob   buy // ,      JavaScript 




Scope for a single expression





Practically the same as for the block but applied to one expression (who would have thought). And accordingly it is not necessary to put curly brackets.

For clarity, consider an example with Bob. So, our Bob fell into childhood and remembered how he responded on the basis of divisibility at the blackboard. He answered on the basis of divisibility by two, except for one example in which he had to answer on the basis of divisibility by 3.

See this example

 //    pred = function(x)!(x%2) //     pred(3) //      Bob-a   pred(2) let(pred = function(x)!(x%3))pred(4) //     




Some habrovchane (namely, those who do not know what a functional PL is) probably noticed that here we defined using let not a variable, but a function, but do not forget that JS is a functional PL, and the function is a full-fledged type.



Let inside for loop



We can also define variables using let for a loop, the only difference from defining with var is that the variable will not be in the scope of the loop and nothing but it.

So let Bob go shopping.

See this example

 list = ["msp430","avr","pic","msp430 Launch pad"] //Bob         var i = 100; // Bob-    for(let i=0;i<list.length;i++){ Bob.buy(list[i]); //Bob     } Bob.say('   ' + i) //100!!! 




Let inside a block





We can use let to define a variable inside a specific block. The difference from the definition of let for a block of code will be that when it starts defining a variable, it will become undefined and only when we assign it a value it will not be undefined. That is, for example. If Bob is asked the square of his favorite number, you can't do that.

See this example

 let i=100 { let i = i * i Bob.say(i) //NaN      let i -   i   i   undefined    undedined  undedined   NaN } 




But nevertheless, you can do this:

See this example

 let i=100 { let m = i * i Bob.say(m) } 




I would be glad to hear comments on the article and adequacy.

Source: https://habr.com/ru/post/113169/



All Articles