-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[2019-10-12] 進階 JavaScript - Closure #8
Labels
JS201
JS201
Comments
function mul(x) {
return (y) => {
console.log('x', x) // 2
console.log('y', y) // 11
return x * y
}
}
mul(2)(11) // 22
mul 是一個 HO function,他回傳了一個 function,所以現在可以再使用另一個 argument 去執行 function |
what are the benefits of HOs?
|
const numbers = [1, 5, 8, 10, 21]
const createAddingFunction = x => arr => {
return arr.map(item => item + x)
}
const numbersPlusOne = createAddingFunction(1)
console.log(numbersPlusOne(numbers))
//[2, 6, 9, 11, 22] |
|
Hooks 運用了 closure 記住 states |
let add = (a) => a*2
let discount = (a) => a*-2
let order = (init)=>{
let money = init
return function(func, num){
money += func(num)
return money
}
}
let initMoney = 1000
let b = order(initMoney)
// 靠 order() return 回來的 scope (function) 產生 closure
// 只有一個 instance : b
console.log(b(add,20))
console.log(b(add,20))
console.log(b(discount,20)) |
// Getting Closure on React Hooks by Shawn Wang
// [Closure] makes it possible for a function to have "private" variables - W3Schools
// 跟到一半
const React =(
function(){
let hooks =[]
let idx = 0
function useState(initVal){
const state = hooks[idx] || initVal
const _idx = idx
const setState = newVal => {
hooks[_idx] = newVal
}
idx++
return [state, setState]
}
function render(Component){
idx = 0
const C = Component()
C.render()
return C
}
function useEffect(cb, depArray){
const oldDeps = hooks[idx]
let hasChange = true
// detect change
if(oldDeps){
hasChange = depArray.some(
(dep, i)=> !Object.is(dep, oldDeps[i]))
}
if(hasChange) cb()
hooks[idx] = depArray
idx++
}
return {useState, render, useEffect}
}
)()
function Component(){
const [count, setCount] = React.useState(1)
const [text, setText] = React.useState('apple')
React.useEffect(()=>{
console.log('jscfffff')
},[count])
return {
render: ()=> console.log({count,text}),
click: ()=> setCount(count + 1),
type: word=> setText(word)
}
}
var App = React.render(Component)
App.click()
var App = React.render(Component)
App.type('foo')
var App = React.render(Component)
|
Example function addAcount(){
let box = 0
return function(val){
box += val
return box
}
}
let addMemoy = addAcount()
let res = addMemoy(100)
res = addMemoy(200)
res = addMemoy(300)
res = addMemoy(400)
console.log('res--->:', res) function foo(){
let box = 0
return function(){
box+=1
return box
}
}
let plus1 = foo()
console.log(plus1())
console.log(plus1())
console.log(plus1())
console.log(plus1())
console.log(plus1()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Closure 是什麼?
Closure 為什麼可以 keep 住一些值
因為 EC 裡面的 scopeChain
(every EC has associated with a scope chain. When control enters an EC, a scope chain is created.
每一個 EC 都有一個 scope chain)
從 ECMAScript 看作用域
Activation Object : AO
Variable Object : VO
AO 和 VO 可以看成是一樣的東西,差別很細微,在 global 是 VO 在 function 是 AO
其實 scopeChain 就是他每一個上層的 Execution Context 裏面的 AO 或 VO 物件
closure 其實就是因為 scopeChain 有 reference 到其他 Execution Context 的 AO 或是 VO,而變數就紀錄在AO 或 VO 裡,也因為 return 把 AO 和 VO 保留下來,所以在離開之後還是可以存取到上層的變數
如果你是以會記住上層資訊的角度來看 closure,那所有的 function 都是 closure
Closure 可以應用在哪裡?
範例:(如此寫的原因)不想讓外面可以
直接賦值
count 這個變數,所以寫了一個 function 給外面操控一個 function (作用域) 裡有一個儲存箱和一個 function 的故事
隱藏著某些資訊
參考資源
所有的函式都是閉包:談 JS 中的作用域與 Closure
The text was updated successfully, but these errors were encountered: