-
Notifications
You must be signed in to change notification settings - Fork 0
/
notion.js
85 lines (72 loc) · 1.95 KB
/
notion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
****************************************************************************************************
* ### Closure
****************************************************************************************************
* Dictionary definition
: A combination of a function and the lexical environment in which it was declared
* When a function is declared inside another function, it remembers the external environment at the time of declaration
* When a function is declared, the lexical environment stores two pieces of information.
* 1. Execution context record of the function
* 2. Outer reference information
* When use the closure?
* 1. Data encaptulation/private
* 2. State maintenance
* 3. Functional Programming, currying
*/
console.log(`\n[ Closure ]`)
{
// In JavaScript, every function is provided with a lexical environment.
let y = 100
function fn() {
let x = 10
console.log(`c.log ## x ##`, x)
console.log(`c.log ## y ##`, y)
}
fn()
}
{
function outer() {
console.log(`c.log ## outer ##`, outer)
let x = 10
function inner() {
console.log(`c.log ## inner ##`, inner)
console.log(`c.log ## x ##`, x)
}
return inner
}
const fn = outer()
console.log(`c.log ## fn(outer()) done ##`)
fn() // inner
}
{
let one
one = 1
function addOne(num) {
console.log(`c.log ## one + num ##`, one + num)
}
addOne(5)
}
{
function makeAdder(x) {
return function (y) {
return x + y
}
}
const add3 = makeAdder(3)
console.log(`c.log ## add3(2) ##`, add3(2))
const add10 = makeAdder(10)
console.log(`c.log ## add10(5) ##`, add10(5))
console.log(`c.log ## add3(1) ##`, add3(1))
}
{
function makeCounter() {
let num = 0
return function () {
return num++
}
}
let counter = makeCounter()
console.log(`c.log ## counter() ##`, counter())
console.log(`c.log ## counter() ##`, counter())
console.log(`c.log ## counter() ##`, counter())
}