-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
38 lines (30 loc) · 884 Bytes
/
test.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
const newProps = {class:'1',disabled:true}
const oldProps = {id:'2',disabled2:false}
/**
* @param {{}}oldProps 表示旧的props
* @param {{}}newProps 表示新的props
* @return {bealean} 当前props是否发生修改
*/
const isPropsChanged = (oldProps,newProps) =>{
// 类型不一致,props肯定发生变化
if(typeof oldProps !== typeof newProps){
return true;
}
// props为对象
if(typeof oldProps === 'object' && typeof newProps === 'object') {
const oldKeys = Object.keys(oldProps);
const newKeys = Object.keys(newProps);
//如果数目不同一定发生变化直接返回true
if(oldKeys.length !== newKeys.length) {
return true;
}
for(let i = 0;i < oldKeys.length;i++) {
const key = oldKeys[i];
if(oldProps[key] !== newProps[key]) {
return true;
}
}
return false;
}
}
console.log(isPropsChanged(oldProps,newProps));