{{JSRef}} {{harmony}}
1つ以上のソースオブジェクトからターゲットのオブジェクトへすべてのすべての独自の列挙プロパティの値をコピーします。戻り値はターゲットオブジェクトです。
構文
Object.assign(target, ...sources)
引数
target
- ターゲットオブジェクト
sources
- ソースオブジェクト
戻り値
ターゲットオブジェクト
説明
Object.assign()
メソッドはソースをオブジェクトからターゲットオブジェクトへenumerableでownなプロパティをコピーするだけです。 ソースをオブジェクトで[[Get]]
を使いターゲットオブジェクトで[[Put]]
を使います。すなわち、gettersとsettersを呼び出します。それゆえ 新しいプロパティをコピーしたり定義してプロパティを割り当てます。これはマージソースがgettersを含んでいるなら新しいプロパティをプロトタイプにマージするのは不適切かもしれません。可算性を含んで、プロパティ定義をプロトタイプにコピーするために、{{jsxref("Object.getOwnPropertyDescriptor()")}} と {{jsxref("Object.defineProperty()")}}が代わりに使われるべきです。
{{jsxref("String")}} と {{jsxref("Symbol")}} プロパティの両方がコピーされます。
エラーの場合、例えば、プロパティが書き込めない場合、{{jsxref("TypeError")}}が起こります。target
オブジェクトは変更されないままです。
Object.assign()
は{{jsxref("null")}}か{{jsxref("undefined")}}ソース値ではスローされないことに注意して下さい。
例
オブジェクトをクローンする
var obj = { a: 1 }; var copy = Object.assign({}, obj); console.log(copy); // { a: 1 }
オブジェクトをマージする
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 } console.log(o1); // { a: 1, b: 2, c: 3 }, target object itself is changed.
シンボル型プロパティをコピーする
var o1 = { a: 1 }; var o2 = { [Symbol('foo')]: 2 }; var obj = Object.assign({}, o1, o2); console.log(obj); // { a: 1, [Symbol("foo")]: 2 }
プロパティを継承して非列挙プロパティはコピされない
var obj = Object.create({ foo: 1 }, { // foo is an inherit property. bar: { value: 2 // bar is a non-enumerable property. }, baz: { value: 3, enumerable: true // baz is an own enumerable property. } }); var copy = Object.assign({}, obj); console.log(copy); // { baz: 3 }
プリミティブ型はオブジェクトにラップされる
var v1 = '123'; var v2 = true; var v3 = 10; var v4 = Symbol('foo') var obj = Object.assign({}, v1, null, v2, undefined, v3, v4); // Primitives will be wrapped, null and undefined will be ignored. // Note, only string wrappers can have own enumerable properties. console.log(obj); // { "0": "1", "1": "2", "2": "3" }
例外が現在進行中のコピータスクを中断する
var target = Object.defineProperty({}, 'foo', { value: 1, writeable: false }); // target.foo is a read-only property Object.assign(target, { bar: 2 }, { foo2: 3, foo: 3, foo3: 3 }, { baz: 4 }); // TypeError: "foo" is read-only // The Exception is thrown when assigning target.foo console.log(target.bar); // 2, the first source was copied successfully. console.log(target.foo2); // 3, the first property of the second source was copied successfully. console.log(target.foo); // 1, exception is thrown here. console.log(target.foo3); // undefined, assign method has finished, foo3 will not be copied. console.log(target.baz); // undefined, the third source will not be copied either.
アクセッサーをコピーする
var obj = { foo: 1, get bar() { return 2; } }; var copy = Object.assign({}, obj); console.log(copy); // { foo: 1, bar: 2 }, the value of copy.bar is obj.bar's getter's return value. // This is an assign function which can copy accessors. function myAssign(target, ...sources) { sources.forEach(source => { Object.defineProperties(target, Object.keys(source).reduce((descriptors, key) => { descriptors[key] = Object.getOwnPropertyDescriptor(source, key); return descriptors; }, {})); }); return target; } var copy = myAssign({}, obj); console.log(copy); // { foo:1, get bar() { return 2 } }
互換性
この互換性はシンボルプロパティをサポートしません。というのも、ES5では シンボルを持っていないからです。:
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); for (var i = 1; i < arguments.length; i++) { var nextSource = arguments[i]; if (nextSource === undefined || nextSource === null) { continue; } nextSource = Object(nextSource); var keysArray = Object.keys(Object(nextSource)); for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { var nextKey = keysArray[nextIndex]; var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); if (desc !== undefined && desc.enumerable) { to[nextKey] = nextSource[nextKey]; } } } return to; } }); }
仕様
仕様 | ステータス | コメント |
---|---|---|
{{SpecName('ES6', '#sec-object.assign', 'Object.assign')}} | {{Spec2('ES6')}} | Initial definition. |
ブラウザ実装状況
{{CompatibilityTable}}
機能 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
基本サポート | {{CompatNo}} | {{CompatGeckoDesktop("34")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
機能 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
基本サポート | {{CompatNo}} | {{CompatNo}} | {{CompatGeckoMobile("34")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
関連情報
- {{jsxref("Object.defineProperties()")}}