goldies
    Preparing search index...

    Function clone

    • Creates a deep, frozen clone of an object.

      Plain objects and arrays are copied recursively and frozen. Dates, Maps and Sets are copied too (Map and Set contents recursively), but freezing does not stop their own methods, such as setTime or set, from changing them.

      Type Parameters

      • T

      Parameters

      • obj: T

        The object to clone.

      Returns T

      The clone of the object.

      const original = { a: 1, b: { c: 2 } };

      const cloned = clone(original);

      console.log(cloned); // { a: 1, b: { c: 2 } }
      console.log(cloned.b === original.b); // false
      console.log(Object.isFrozen(cloned.b)); // true, so `cloned.b.c = 4` throws in strict mode

      Use the built-in structuredClone(obj), which also copies objects that refer to themselves. Unlike clone, it doesn't freeze the copy, and it throws on functions. Will be removed in goldies 6.0.