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

深拷贝那里有点问题 #21

Open
hua03 opened this issue May 8, 2021 · 0 comments
Open

深拷贝那里有点问题 #21

hua03 opened this issue May 8, 2021 · 0 comments

Comments

@hua03
Copy link

hua03 commented May 8, 2021

深拷贝那里,不能直接将result赋值为空对象,这样会破坏对象的原型链,而且修改了对象。
可以改成这样:

result = new obj.constructor()

利用原对象的构造函数生成新对象,然后用for in 循环给对象添加属性。不过这种方式也存在弊端,如果构造函数需要接收参数做计算,就会导致执行时报错。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant