Today I Learned - Rocky Kev

TIL Lexical Scoping

POSTED ON:

TAGS:

Lexical scoping is a part of Closures.

Lexical scoping describes how a parser resolves variable names when functions are nested. Lexical refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available. Nested functions have access to variables declared in their outer scope.

function initialize() {

var name = 'Mozilla'; // name is a local variable created by initialize

function displayName() { // displayName() is the inner function, a closure
alert(name); // the variable declared in the parent function
}

displayName(); // will fire the displayName function

}

initialize();

This is the example in the MDN.
The variable name will stay stay and remain available for us.

Via:
MDN Web Docs: Closures


Related TILs

Tagged:

TIL what is npm Script

Despite their high usage they are not particularly well optimized and add about 400ms of overhead. In this article we were able to bring that down to ~22ms.

TIL fancy methods to transform Javascript Objects

You can use Object.entries(), Object.keys(), Object.fromEntries()...

TIL how to hide your JS code

ONE THING TO NOTE: Encrypting a script is stronger than obfuscation, both methods are still not adequate to protect secret content. Honestly, I don't think it's worth it on a production site, and instead just go with pure server-side if you want security. But it's fascinating.