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 returns
return {}
ใreturn null
๏ผreturn 1
๏ผreturn true
What 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.