Defines a type T that is cloneable, providing a method to create a new,
independent instance of itself.
The Clone interface ensures that any type implementing it can produce
a duplicate of its current instance via the clone method. The type T
represents the specific type of the implementing instance.
Similar to Rust's Clone trait, this interface is intended for types that
need explicit duplication logic (e.g., objects or complex structures),
as opposed to Primitive types, which are implicitly copied by value.
Unlike reference types that might share state, a clone
implementation should produce a distinct instance, though the depth of the copy
(shallow or deep) is left to the implementor.
T is the type of the instance that implements this interface.
Typically, this is the class or type itself (e.g., a class MyType would
implement Clone<MyType>).
constoriginal = newMyType(42); constduplicate = original.clone(); expect(duplicate.value).toBe(original.value); // same value expect(duplicate).not.toBe(original); // different reference
Defines a type
T
that is cloneable, providing a method to create a new, independent instance of itself.The Clone interface ensures that any type implementing it can produce a duplicate of its current instance via the
clone
method. The typeT
represents the specific type of the implementing instance.Similar to Rust's
Clone
trait, this interface is intended for types that need explicit duplication logic (e.g., objects or complex structures), as opposed to Primitive types, which are implicitly copied by value. Unlike reference types that might share state, a clone implementation should produce a distinct instance, though the depth of the copy (shallow or deep) is left to the implementor.T
is the type of the instance that implements this interface. Typically, this is the class or type itself (e.g., a classMyType
would implementClone<MyType>
).Example