This is an experimental technology, part of the Harmony (ECMAScript 6) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.
Summary
The Object.assign()
method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
Syntax
Object.assign(target, ...sources)
Parameters
-
target
- The target object.
-
sources
- The source object(s).
Return value
The target object gets returned.
Description
The Object.assign
method only copies enumerable and own properties from a source object to a target object. It uses [[Get]]
on the source and [[Put]]
on the target, so it will invoke getters and setters. Therefore it assigns properties versus just copying or defining new properties.
Both, String
and Symbol
properties are copied.
In case of an error, for example if a property is non-writable, a TypeError
will be raised, but assignment continues and the error is thrown at the end.
Note that Object.assign
does not throw on null
or undefined
source values.
Examples
Example: Cloning an object
var obj = { a: 1 }; var copy = Object.assign({}, obj);
Example: Merging objects
var o1 = { a: 1 }; var o2 = { b: 2 }; var o3 = { c: 3 }; var obj = Object.assign(o1, o2, o3); console.log(obj); // { a: 1, b: 2, c: 3 }
Example: Exception cases
Object.assign(window, {undefined: true, foo: "foo"}, {bar: "bar"}); // TypeError: "undefined" is read-only console.log(window.foo); // "foo", exception is thrown after completing this source console.log(window.bar); // undefined, all later sources will be ignored
Polyfill
This polyfill doesn't support symbol properties, since ES5 doesn't have symbols anyway:
if (!Object.assign) { Object.defineProperty(Object, "assign", { enumerable: false, configurable: true, writable: true, value: function(target, firstSource) { "use strict"; if (target === undefined || target === null) throw new TypeError("Cannot convert first argument to object"); var to = Object(target); var hasPendingException = false; var pendingException; for (var i = 1; i < arguments.length; i++) { var nextSource = arguments[i]; if (nextSource === undefined || nextSource === null) continue; var keysArray = Object.keys(Object(nextSource)); for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { var nextKey = keysArray[nextIndex]; try { var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); if (desc !== undefined && desc.enumerable) to[nextKey] = nextSource[nextKey]; } catch (e) { if (!hasPendingException) { hasPendingException = true; pendingException = e; } } } if (hasPendingException) throw pendingException; } return to; } }); }
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 6 (ECMA-262) The definition of 'Object.assign' in that specification. |
Draft | Initial definition. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | Not supported | 34 (34) | Not supported | Not supported | Not supported |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | Not supported | Not supported | 34.0 (34) | Not supported | Not supported | Not supported |