Appearance
14.装包和拆包
一.装包
type Proxy<T> = {
get():T,
set(value:T):void
}
type Proxify<T> = {
[P in keyof T]: Proxy<T[P]>
}
let props = {
name: 'zhufeng',
age: 11
}
function proxify<T>(obj:T):Proxify<T>{
let result = {} as Proxify<T>;
for(let key in obj){
let value = obj[key];
result[key] = {
get(){
return value
},
set:(newValue)=>value = newValue
}
}
return result
}
let proxpProps = proxify(props);
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
二.拆包
function unProxify<T>(proxpProps:Proxify<T>):T{
let result = {} as T;
for(let key in proxpProps){
let value = proxpProps[key];
result[key] = value.get()
}
return result
}
let proxy = unProxify(proxpProps)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9