Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

js中如何实现this的深拷贝?

如果使用for循环深度遍历,会导致死循环;
或许可以使用 loadsh库的deepclone来实现,还有什么方式吗,原因是什么?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

生产中可以使用: circular-json 库来实现,避免JSON.stringify, JSON.parse处理循环报错的问题;
实现原理是:转成序列化后,循环引用的对象使用一个特殊的字符器标记来代替,parse的过程再把标记转成要引用的那个对象(this)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...