Object.assign
时间: 2023-11-05 09:03:28 浏览: 81
Object.assign() is a method in JavaScript that is used to copy the values of all enumerable properties of one or more source objects to a target object. It returns the target object with the updated properties. If there are multiple sources, the properties of the later sources will overwrite the properties of the earlier sources.
Syntax:
```
Object.assign(target, ...sources)
```
- `target`: The target object to which the properties are to be copied.
- `sources`: One or more source objects whose properties are to be copied to the target object.
Example:
```
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const result = Object.assign(target, source);
console.log(target); // { a: 1, b: 4, c: 5 }
console.log(result); // { a: 1, b: 4, c: 5 }
```
In this example, the `Object.assign()` method copies the properties from the `source` object to the `target` object. The `b` property of the `target` object is overwritten by the `b` property of the `source` object. The `result` variable holds the updated `target` object.
阅读全文