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

JS深拷贝碰到的问题--后续 #156

Open
mishe opened this issue Mar 20, 2017 · 0 comments
Open

JS深拷贝碰到的问题--后续 #156

mishe opened this issue Mar 20, 2017 · 0 comments

Comments

@mishe
Copy link
Owner

mishe commented Mar 20, 2017

上期 #155 讲了js 深拷贝的简单实现方法,还留了个尾巴,没有解决对象重复引用的问题

重复引用会有什么问题

例子1:

var obj={
a:1
}
obj.b=obj

例子2:

o1={a:1};o2={a:o1};o3={a:o2};o1.a=o3
}

这时去做深拷贝,会陷入无限递归。

怎么解决

对象本身就是树形结构,可以用一个数组来保存当前枝叶链上的所有object,如果下层枝叶又引用上层的obj,那就直接赋值,而不是采用递归,从而打破无限递归的深渊。

【具体实现代码】

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