Skip to content
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

修复使用数组的 pop 和 splice 方法操作数据时,结果错误的问题 #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions content/toys/deepClone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ function produce(baseState, fn) {
// 否则直接 copy[key] = newValue 的话外部拿到的对象是个 proxy
copy[key] = isProxy(newValue) ? newValue[MY_IMMER] : newValue
return true
}
},
deleteProperty(target, key) {
const copy = getCopy(target)
delete copy[key]
return true
},
}

const getProxy = data => {
Expand Down Expand Up @@ -99,12 +104,14 @@ const state = {
}
}
},
data: [1]
data: [1, 2, 3]
}

const data = produce(state, draftState => {
draftState.info.age = 26
draftState.info.career.first.name = '222'
draftState.data.splice(0, 1)
draftState.data.pop()
})

console.log(data, state)
Expand Down