首页 前端知识 lodas deepClone or deepClone from clone_deep npm returns same instance

lodas deepClone or deepClone from clone_deep npm returns same instance

2025-03-12 12:03:32 前端知识 前端哥 117 317 我要收藏

题意:使用 lodash 的 _.cloneDeep 函数或 clone-deep npm 包中的 cloneDeep 函数时,返回了相同的实例

问题背景:

What I'm observing now is making me question reality. Of course I'm working with Javasript.

我现在观察到的现象让我对现实产生了怀疑。当然,我正在使用JavaScript工作。

I have following code (clone-deep npm package v 4.0.1)

我有以下代码(clone-deep npm包版本4.0.1)

    let deepCloned = cloneDeep(obj);
    
    if (deepCloned === obj) {
        debugger;
    }

and of course in some cases the debugger is hit. When I change it to lo dashes implementation

当然,在某些情况下,调试器会被触发。当我将其更改为lodash的实现时

    let deepCloned = _.cloneDeep(obj);
    
    if (deepCloned === obj) {
        debugger;
    }

same behavior can be observed. So I have to be missing something really fundamental here. What is it?

可以观察到相同的行为。所以我肯定在这里遗漏了一些非常基础的东西。那是什么

问题解决:

from the package page: https://www.npmjs.com/package/clone-deep#heads-up

从软件包页面

Heads up!  注意!

The last argument specifies whether or not to clone instances (objects that are from a custom class or are not created by the Object constructor. This value may be true or the function use for cloning instances.

最后一个参数指定是否克隆实例(即来自自定义类或不是由Object构造函数创建的对象)。此值可以是true或用于克隆实例的函数。

When an instanceClone function is provided, it will be invoked to clone objects that are not "plain" objects (as defined by isPlainObjectisPlainObject). If instanceClone is not specified, this library will not attempt to clone non-plain objects, and will simply copy the object reference.

当提供了instanceClone函数时,将调用它来克隆不是“纯”对象(如isPlainObject所定义的)的对象。如果未指定instanceClone,则此库将不会尝试克隆非纯对象,而只是复制对象引用。

I do not use that package myself and you did not mention how the "obj" is created so I cannot reproduce your exact problem.

我自己不使用那个软件包,而且您没有提到“obj”是如何创建的,所以我无法重现您遇到的确切问题。

转载请注明出处或者链接地址:https://www.qianduange.cn//article/23365.html
标签
评论
会员中心 联系我 留言建议 回顶部
复制成功!