Skip to content

Latest commit

 

History

History
69 lines (55 loc) · 1.9 KB

File metadata and controls

69 lines (55 loc) · 1.9 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs ms.assetid caps.latest.revision author ms.author manager
Object.assign Function (Object) (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
JavaScript
TypeScript
DHTML
2dd6b312-dcd3-414e-8d53-088c6341c46d
4
mikejo5000
mikejo
ghogen

Object.assign Function (Object) (JavaScript)

Copies the values from one or more source objects to a target object.

Syntax

Object.assign(target, ...sources );  

Parameters

target
Required. A object to which enumerable properties are copied.

...sources
Required. One or more objects from which enumerable properties are copied.

Exceptions

This function throws a TypeError if there is an assignment error, which terminates the copying operation. A TypeError will be thrown if a target property is not writable.

Remarks

This function returns the target object. Only enumerable own properties are copied from the source object to the target object. You can use this function to merge or clone objects.

null or undefined sources are treated like empty objects and contribute nothing to the target object.

Example

The following code example shows how to merge an object using Object.assign.

var first = { name: "Bob" };  
var last = { lastName: "Smith" };  
  
var person = Object.assign(first, last);  
console.log(person);  
  
// Output:  
// { name: "Bob", lastName: "Smith" }   

Example

The following example shows how to clone an object using Object.assign.

var obj = { person: "Bob Smith"};  
var clone = Object.assign({}, obj);  

Requirements

[!INCLUDEjsv12]