• The objAssign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.

    Properties in the target object are overwritten by properties in the sources if they have the same key. Later sources' properties overwrite earlier ones.

    The objAssign() method only copies enumerable and own properties from a source object to a target object. It uses Get on the source and Set on the target, so it will invoke getters and setters. Therefore it assigns properties, versus copying or defining new properties. This may make it unsuitable for merging new properties into a prototype if the merge sources contain getters.

    For copying property definitions (including their enumerability) into prototypes, use objGetOwnPropertyDescriptor and objDefineProp instead.

    Both String and Symbol properties are copied.

    In case of an error, for example if a property is non-writable, a TypeError is raised, and the target object is changed if any properties are added before the error is raised.

    Type Parameters

    • T extends {}
    • U

    Parameters

    • target: T
    • source: U

    Returns T & U

    Example

    const obj = { a: 1 };
    const copy = objAssign({}, obj);
    console.log(copy); // { a: 1 }

    const o1 = { a: 1 };
    const o2 = { b: 2 };
    const o3 = { c: 3 };

    const obj = objAssign(o1, o2, o3);
    console.log(obj); // { a: 1, b: 2, c: 3 }
    console.log(o1); // { a: 1, b: 2, c: 3 }, target object itself is changed.
  • The objAssign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.

    Properties in the target object are overwritten by properties in the sources if they have the same key. Later sources' properties overwrite earlier ones.

    The objAssign() method only copies enumerable and own properties from a source object to a target object. It uses Get on the source and Set on the target, so it will invoke getters and setters. Therefore it assigns properties, versus copying or defining new properties. This may make it unsuitable for merging new properties into a prototype if the merge sources contain getters.

    For copying property definitions (including their enumerability) into prototypes, use objGetOwnPropertyDescriptor and objDefineProp instead.

    Both String and Symbol properties are copied.

    In case of an error, for example if a property is non-writable, a TypeError is raised, and the target object is changed if any properties are added before the error is raised.

    Type Parameters

    • T extends {}
    • U
    • V

    Parameters

    • target: T
    • source1: U
    • source2: V

    Returns T & U & V

    Example

    const obj = { a: 1 };
    const copy = objAssign({}, obj);
    console.log(copy); // { a: 1 }

    const o1 = { a: 1 };
    const o2 = { b: 2 };
    const o3 = { c: 3 };

    const obj = objAssign(o1, o2, o3);
    console.log(obj); // { a: 1, b: 2, c: 3 }
    console.log(o1); // { a: 1, b: 2, c: 3 }, target object itself is changed.
  • The objAssign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.

    Properties in the target object are overwritten by properties in the sources if they have the same key. Later sources' properties overwrite earlier ones.

    The objAssign() method only copies enumerable and own properties from a source object to a target object. It uses Get on the source and Set on the target, so it will invoke getters and setters. Therefore it assigns properties, versus copying or defining new properties. This may make it unsuitable for merging new properties into a prototype if the merge sources contain getters.

    For copying property definitions (including their enumerability) into prototypes, use objGetOwnPropertyDescriptor and objDefineProp instead.

    Both String and Symbol properties are copied.

    In case of an error, for example if a property is non-writable, a TypeError is raised, and the target object is changed if any properties are added before the error is raised.

    Type Parameters

    • T extends {}
    • U
    • V
    • W

    Parameters

    • target: T
    • source1: U
    • source2: V
    • source3: W

    Returns T & U & V & W

    Example

    const obj = { a: 1 };
    const copy = objAssign({}, obj);
    console.log(copy); // { a: 1 }

    const o1 = { a: 1 };
    const o2 = { b: 2 };
    const o3 = { c: 3 };

    const obj = objAssign(o1, o2, o3);
    console.log(obj); // { a: 1, b: 2, c: 3 }
    console.log(o1); // { a: 1, b: 2, c: 3 }, target object itself is changed.
  • The objAssign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.

    Properties in the target object are overwritten by properties in the sources if they have the same key. Later sources' properties overwrite earlier ones.

    The objAssign() method only copies enumerable and own properties from a source object to a target object. It uses Get on the source and Set on the target, so it will invoke getters and setters. Therefore it assigns properties, versus copying or defining new properties. This may make it unsuitable for merging new properties into a prototype if the merge sources contain getters.

    For copying property definitions (including their enumerability) into prototypes, use objGetOwnPropertyDescriptor and objDefineProp instead.

    Both String and Symbol properties are copied.

    In case of an error, for example if a property is non-writable, a TypeError is raised, and the target object is changed if any properties are added before the error is raised.

    Parameters

    • target: object
    • Rest ...sources: any[]

    Returns any

    Example

    const obj = { a: 1 };
    const copy = objAssign({}, obj);
    console.log(copy); // { a: 1 }

    const o1 = { a: 1 };
    const o2 = { b: 2 };
    const o3 = { c: 3 };

    const obj = objAssign(o1, o2, o3);
    console.log(obj); // { a: 1, b: 2, c: 3 }
    console.log(o1); // { a: 1, b: 2, c: 3 }, target object itself is changed.