365 days of clocking

๐Ÿ”ฅ creation is not easy, we help uncle B stack of a key three

  • How to use closures to complete library encapsulation ๐Ÿ“บ Billbill video ๐Ÿ“š Gold Digging Manuscript ๐Ÿฑ Github
  • [Talk about closures and real-time functions] ๐Ÿ“บ Billbill video ๐Ÿ“š Nugget manuscript ๐Ÿฑ Github
  • ๐Ÿ“บ Billbill video ๐Ÿ“š Digger manuscript ๐Ÿฑ Github
  • The relationship between closures and curated, partial application functions ๐Ÿ“บ Billbill video ๐Ÿ“š Denver Manuscript ๐Ÿฑ Github
  • How to make lazy functions with closures? ๐Ÿ“บ Billbill video ๐Ÿ“š Nuggets Script ๐Ÿฑ Github
  • What is a closure and how to create one? ๐Ÿ“บ Billbill video ๐Ÿ“š Nuggets Manuscript ๐Ÿฑ Github
  • [new a constructor if the function returnsreturn {} ใ€ return null ๏ผŒ return 1 ๏ผŒ return trueWhat happens? ]๐Ÿ“บ Billbill video๐Ÿ“š Gold digging manuscript๐Ÿฑ making
  • What happens to a new function? ๐Ÿ“บ Billbill video ๐Ÿ“š Nuggets Script ๐Ÿฑ Github
  • What are the ways to determine data types? ๐Ÿ“บ Billbill video ๐Ÿ“š Nuggets Script ๐Ÿฑ Github
  • [How much storage space does Number() have? What if the background sends a Number that exceeds the maximum limit?] ๐Ÿ“บ Billbill video ๐Ÿ“š Nuggets Script ๐Ÿฑ Github
  • [0.1 + 0.2 === 0.3? Why? How to solve?] ๐Ÿ“บ Billbill video ๐Ÿ“š Nuggets Script ๐Ÿฑ Github
  • How are JS integers represented? ๐Ÿ“บ Billbill video ๐Ÿ“š Nuggets transcript

Knowledge point explanation

Block-level scopes were introduced in Javascript to address an important design flaw in Javascript.

The variable promotion feature leads to a lot of counterintuitive code

What is variable promotion?

(function() {
 ย console.log(v)
 ย var v = 123
})()
โ€‹
Copy the code

Js promotes variables declared by var and functions declared by keyword functions to the top of the current scope. The assignment statement waits for assignment in place.

The advantage of variable promotion is to reduce the difficulty of writing programs.

Why say so, in fact, the original program is written when there is a clear order requirements.

For example, let’s look at a prehistoric language called Pascal

Complete program structure

It’s like Honor of Kings not having to go home to buy equipment to make it easier to operate.

Counterintuitive

But there are problems with this approach. After all, you’ve arbitrarily modified the execution logic.

It is mainly divided into two aspects: line and plane

The inner variable overrides the outer variable

var tmp = new Date();
โ€‹
function f() {
 ย console.log(tmp);
 ย if (false) {
 ย  ย var tmp = 'hello world';
  }
}
โ€‹
f(); // undefined
Copy the code

The above program, the global definition of the variable, because the program behind the definition of the same name variable program, unexpectedly disappeared.

Variables that should have been destroyed were not destroyed

This is actually known as the circular trap problem. I didn’t say it in detail the other day.

var s = 'hello'; for (var i = 0; i < s.length; i++) { console.log(s[i]); } console.log(i); / / 5Copy the code

The interview guide

When it comes to the effect of block-level scope, some people will talk about the phenomenon first, not the essence. It is suggested to analyze this problem from the perspective of perfecting language defects.