aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/object/assign/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/object/assign/index.html
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/object/assign/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/object/assign/index.html321
1 files changed, 321 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/assign/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/assign/index.html
new file mode 100644
index 0000000000..13cc1854d6
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/object/assign/index.html
@@ -0,0 +1,321 @@
+---
+title: Object.assign()
+slug: Web/JavaScript/Reference/Global_Objects/Object/assign
+tags:
+ - ECMAScript 2015
+ - JavaScript
+ - Method
+ - Object
+ - Object.assign
+ - polyfill
+translation_of: Web/JavaScript/Reference/Global_Objects/Object/assign
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>Object.assign()</strong></code> 方法用于将所有可枚举属性的值从一个或多个源对象分配到目标对象。它将返回目标对象。</p>
+
+<div>{{EmbedInteractiveExample("pages/js/object-assign.html")}}</div>
+
+
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox notranslate"><code>Object.assign(<var>target</var>, ...<var>sources</var>)</code></pre>
+
+<h3 id="Parameters" name="Parameters">参数</h3>
+
+<dl>
+ <dt><code>target</code></dt>
+ <dd>目标对象。</dd>
+ <dt><code>sources</code></dt>
+ <dd>源对象。</dd>
+</dl>
+
+<h3 id="Return_value" name="Return_value">返回值</h3>
+
+<p>目标对象。</p>
+
+<h2 id="描述">描述</h2>
+
+<p>如果目标对象中的属性具有相同的键,则属性将被源对象中的属性覆盖。后面的源对象的属性将类似地覆盖前面的源对象的属性。</p>
+
+<p><code>Object.assign</code> 方法只会拷贝源对象自身的并且可枚举的属性到目标对象。该方法使用源对象的<code>[[Get]]</code>和目标对象的<code>[[Set]]</code>,所以它会调用相关 getter 和 setter。因此,它分配属性,而不仅仅是复制或定义新的属性。如果合并源包含getter,这可能使其不适合将新属性合并到原型中。为了将属性定义(包括其可枚举性)复制到原型,应使用{{jsxref("Object.getOwnPropertyDescriptor()")}}和{{jsxref("Object.defineProperty()")}} 。</p>
+
+<p>{{jsxref("String")}}类型和 {{jsxref("Symbol")}} 类型的属性都会被拷贝。</p>
+
+<p>在出现错误的情况下,例如,如果属性不可写,会引发{{jsxref("TypeError")}},如果在引发错误之前添加了任何属性,则可以更改<code>target</code>对象。</p>
+
+<div class="blockIndicator note">
+<p>注意,<code>Object.assign</code> 不会在那些<code>source</code>对象值为 {{jsxref("null")}} 或 {{jsxref("undefined")}} 的时候抛出错误。</p>
+</div>
+
+<h2 id="Polyfill_2">Polyfill</h2>
+
+<p>这个 <a href="https://wiki.developer.mozilla.org/en-US/docs/Glossary/Polyfill">polyfill</a> 不支持 symbol 属性, 由于 ES5 中本来就不存在 symbols :</p>
+
+<pre class="notranslate">if (typeof Object.assign !== 'function') {
+ // Must be writable: true, enumerable: false, configurable: true
+ Object.defineProperty(Object, "assign", {
+ value: function assign(target, varArgs) { // .length of function is 2
+ 'use strict';
+ if (target === null || target === undefined) {
+ throw new TypeError('Cannot convert undefined or null to object');
+ }
+
+ var to = Object(target);
+
+ for (var index = 1; index &lt; arguments.length; index++) {
+ var nextSource = arguments[index];
+
+ if (nextSource !== null &amp;&amp; nextSource !== undefined) {
+ for (var nextKey in nextSource) {
+ // Avoid bugs when hasOwnProperty is shadowed
+ if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
+ to[nextKey] = nextSource[nextKey];
+ }
+ }
+ }
+ }
+ return to;
+ },
+ writable: true,
+ configurable: true
+ });
+}</pre>
+
+<h2 id="Examples" name="Examples">示例</h2>
+
+<h3 id="Example_Cloning_an_object" name="Example:_Cloning_an_object">复制一个对象</h3>
+
+<pre class="brush: js notranslate">const obj = { a: 1 };
+const copy = Object.assign({}, obj);
+console.log(copy); // { a: 1 }
+</pre>
+
+<h3 id="Deep_Clone" name="Deep_Clone">深拷贝问题</h3>
+
+<p>针对深拷贝,需要使用其他办法,因为 <code>Object.assign()</code>拷贝的是(可枚举)属性值。</p>
+
+<p>假如源值是一个对象的引用,它仅仅会复制其引用值。</p>
+
+<pre class="brush: js notranslate">const log = console.log;
+
+function test() {
+ 'use strict';
+ let obj1 = { a: 0 , b: { c: 0}};
+ let obj2 = Object.assign({}, obj1);
+ log(JSON.stringify(obj2));
+  // { a: 0, b: { c: 0}}
+
+ obj1.a = 1;
+ log(JSON.stringify(obj1));
+  // { a: 1, b: { c: 0}}
+ log(JSON.stringify(obj2));
+  // { a: 0, b: { c: 0}}
+
+ obj2.a = 2;
+ log(JSON.stringify(obj1));
+  // { a: 1, b: { c: 0}}
+ log(JSON.stringify(obj2));
+  // { a: 2, b: { c: 0}}
+
+ obj2.b.c = 3;
+ log(JSON.stringify(obj1));
+  // { a: 1, b: { c: 3}}
+ log(JSON.stringify(obj2));
+  // { a: 2, b: { c: 3}}
+
+ // Deep Clone
+ obj1 = { a: 0 , b: { c: 0}};
+ let obj3 = JSON.parse(JSON.stringify(obj1));
+ obj1.a = 4;
+ obj1.b.c = 4;
+ log(JSON.stringify(obj3));
+ // { a: 0, b: { c: 0}}
+}
+
+test();
+</pre>
+
+<h3 id="Example_Merging_objects" name="Example:_Merging_objects">合并对象</h3>
+
+<pre class="brush: js notranslate">const o1 = { a: 1 };
+const o2 = { b: 2 };
+const o3 = { c: 3 };
+
+const obj = Object.assign(o1, o2, o3);
+console.log(obj); // { a: 1, b: 2, c: 3 }
+console.log(o1); // { a: 1, b: 2, c: 3 }, 注意目标对象自身也会改变。
+</pre>
+
+<h3 id="合并具有相同属性的对象">合并具有相同属性的对象</h3>
+
+<pre class="brush: js notranslate">const o1 = { a: 1, b: 1, c: 1 };
+const o2 = { b: 2, c: 2 };
+const o3 = { c: 3 };
+
+const obj = Object.assign({}, o1, o2, o3);
+console.log(obj); // { a: 1, b: 2, c: 3 }</pre>
+
+<p>属性被后续参数中具有相同属性的其他对象覆盖。</p>
+
+<h3 id="Example_Symbol_properties" name="Example:_Symbol_properties">拷贝 symbol 类型的属性</h3>
+
+<pre class="brush: js notranslate">const o1 = { a: 1 };
+const o2 = { [Symbol('foo')]: 2 };
+
+const obj = Object.assign({}, o1, o2);
+console.log(obj); // { a : 1, [Symbol("foo")]: 2 } (cf. bug 1207182 on Firefox)
+Object.getOwnPropertySymbols(obj); // [Symbol(foo)]</pre>
+
+<h3 id="Example_Only_own_enumerable_properties" name="Example:_Only_own_enumerable_properties">继承属性和不可枚举属性是不能拷贝的</h3>
+
+<pre class="brush: js notranslate">const obj = Object.create({foo: 1}, { // foo 是个继承属性。
+ bar: {
+ value: 2 // bar 是个不可枚举属性。
+ },
+ baz: {
+ value: 3,
+ enumerable: true // baz 是个自身可枚举属性。
+ }
+});
+
+const copy = Object.assign({}, obj);
+console.log(copy); // { baz: 3 }
+</pre>
+
+<h3 id="Example_Primitives" name="Example:_Primitives">原始类型会被包装为对象</h3>
+
+<pre class="brush: js notranslate">const v1 = "abc";
+const v2 = true;
+const v3 = 10;
+const v4 = Symbol("foo")
+
+const obj = Object.assign({}, v1, null, v2, undefined, v3, v4);
+// 原始类型会被包装,null 和 undefined 会被忽略。
+// 注意,只有字符串的包装对象才可能有自身可枚举属性。
+console.log(obj); // { "0": "a", "1": "b", "2": "c" }</pre>
+
+<h3 id="Example_Exceptions" name="Example:_Exceptions">异常会打断后续拷贝任务</h3>
+
+<pre class="brush: js notranslate">const target = Object.defineProperty({}, "foo", {
+ value: 1,
+ writable: false
+}); // target 的 foo 属性是个只读属性。
+
+Object.assign(target, {bar: 2}, {foo2: 3, foo: 3, foo3: 3}, {baz: 4});
+// TypeError: "foo" is read-only
+// 注意这个异常是在拷贝第二个源对象的第二个属性时发生的。
+
+console.log(target.bar); // 2,说明第一个源对象拷贝成功了。
+console.log(target.foo2); // 3,说明第二个源对象的第一个属性也拷贝成功了。
+console.log(target.foo); // 1,只读属性不能被覆盖,所以第二个源对象的第二个属性拷贝失败了。
+console.log(target.foo3); // undefined,异常之后 assign 方法就退出了,第三个属性是不会被拷贝到的。
+console.log(target.baz); // undefined,第三个源对象更是不会被拷贝到的。
+</pre>
+
+<h3 id="Example_Copy_accessors" name="Example:_Copy_accessors">拷贝访问器</h3>
+
+<pre class="brush: js notranslate">const obj = {
+ foo: 1,
+ get bar() {
+ return 2;
+ }
+};
+
+let copy = Object.assign({}, obj);
+console.log(copy); // { foo: 1, bar: 2 } copy.bar的值来自obj.bar的getter函数的返回值
+
+// 下面这个函数会拷贝所有自有属性的属性描述符
+function completeAssign(target, ...sources) {
+ sources.forEach(source =&gt; {
+ let descriptors = Object.keys(source).reduce((descriptors, key) =&gt; {
+ descriptors[key] = Object.getOwnPropertyDescriptor(source, key);
+ return descriptors;
+ }, {});
+
+  // Object.assign 默认也会拷贝可枚举的Symbols
+ Object.getOwnPropertySymbols(source).forEach(sym =&gt; {
+ let descriptor = Object.getOwnPropertyDescriptor(source, sym);
+ if (descriptor.enumerable) {
+ descriptors[sym] = descriptor;
+ }
+ });
+ Object.defineProperties(target, descriptors);
+ });
+ return target;
+}
+
+copy = completeAssign({}, obj);
+console.log(copy);
+// { foo:1, get bar() { return 2 } }
+</pre>
+
+<h2 id="Polyfill" name="Polyfill">Polyfill</h2>
+
+<p>此{{Glossary("Polyfill","polyfill")}}不支持 symbol 属性,因为ES5 中根本没有 symbol :</p>
+
+<pre class="brush: js notranslate">if (typeof Object.assign != 'function') {
+ // Must be writable: true, enumerable: false, configurable: true
+ Object.defineProperty(Object, "assign", {
+ value: function assign(target, varArgs) { // .length of function is 2
+ 'use strict';
+ if (target == null) { // TypeError if undefined or null
+ throw new TypeError('Cannot convert undefined or null to object');
+ }
+
+ let to = Object(target);
+
+ for (var index = 1; index &lt; arguments.length; index++) {
+ var nextSource = arguments[index];
+
+ if (nextSource != null) { // Skip over if undefined or null
+ for (let nextKey in nextSource) {
+ // Avoid bugs when hasOwnProperty is shadowed
+ if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
+ to[nextKey] = nextSource[nextKey];
+ }
+ }
+ }
+ }
+ return to;
+ },
+ writable: true,
+ configurable: true
+ });
+}</pre>
+
+<h2 id="Specifications" name="Specifications">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范名称</th>
+ <th scope="col">规范状态</th>
+ <th scope="col">备注</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-object.assign', 'Object.assign')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-object.assign', 'Object.assign')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容</h2>
+
+<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
+
+<p>{{Compat("javascript.builtins.Object.assign")}}</p>
+
+<h2 id="See_also" name="See_also">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Object.defineProperties()")}}</li>
+ <li><a href="/zh-CN/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">属性的可枚举性和所有权</a></li>
+</ul>