1

jQuery.extend安全吗circular reference

如何避免 Javascript 中的循环引用(在克隆或递归检查时)?仅在其属性列表中检查当前目标的存在是不够的,因为它可能引用了一些外部对象。

一种选择是保留另一个objects到目前为止所有已获取的列表。但这会增加内存消耗并要求停止脚本吗?

而且我不想在工作线程中移动克隆操作。

4

1 回答 1

0

老问题,但我今天正在寻找这个 - 答案是:不。

https://api.jquery.com/jquery.extend/

在深度扩展中,Object 和 Array 被扩展,但基本类型(如 String、Boolean 和 Number)上的对象包装器则没有。深度扩展循环数据结构将导致错误

对于不属于此行为的需求,请编写自定义扩展方法,或使用 lodash 之类的库。

lodash 文档不是很清楚,但 _.cloneDeep 支持克隆循环引用。

https://lodash.com/docs/4.17.10#cloneDeep

你最好使用像 lodash 这样聪明的东西,因为我认为它会正确检测多次引用的所有对象,并创建整个对象图的真正克隆,并且所有循环引用都完好无损。

然而,这是一个简单的深度克隆,它使用简单的对象堆栈(它在 TypeScript 中)简单地忽略了循环引用:

public static DeepClone(source: any, stack: any[] = null): any
{
    if (!source)
    {
        return source;
    }

    var result;

    result = Array.isArray(source) ? [] : {};

    for (var k in source)
    {
        var v = source[k];
        if (typeof v === "object") 
        {
            stack = stack || [];

            // Just ignore circular references? Or, to be clever, maintain a 
            // dictionary of object references from the original graph linked to
            // the new objects in the cloned graph, to preserve the full graph.
            if (stack.indexOf(source) >= 0)
            {
                return null;
            }

            stack.push(source);

            result[k] = this.DeepClone(v, stack);

            stack.pop();
        }
        else
        {
            result[k] = v;
        }
    }

    return result;
}
于 2018-10-14T08:19:04.767 回答