blob: 52875fee26330c2efa560aeca2f6a7b19f66ffcf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
---
title: Object.getOwnPropertyDescriptors()
slug: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptors
translation_of: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptors
---
<div>{{JSRef}}</div>
<p><code><strong>Object.getOwnPropertyDescriptors()</strong></code> 方法用来获取一个对象的所有自身属性的描述符。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">Object.getOwnPropertyDescriptors(<var>obj</var>)</pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>obj</code></dt>
<dd>任意对象</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>所指定对象的所有自身属性的描述符,如果没有任何自身属性,则返回空对象。</p>
<dl>
</dl>
<h2 id="示例">示例</h2>
<h3 id="浅拷贝一个对象">浅拷贝一个对象</h3>
<p>{{jsxref("Object.assign()")}} 方法只能拷贝源对象的可枚举的自身属性,同时拷贝时无法拷贝属性的特性们,而且访问器属性会被转换成数据属性,也无法拷贝源对象的原型,该方法配合 {{jsxref("Object.create()")}} 方法可以实现上面说的这些。</p>
<pre class="brush: js">Object.create(
Object.getPrototypeOf(obj),
Object.getOwnPropertyDescriptors(obj)
);
</pre>
<h3 id="创建子类">创建子类</h3>
<p>创建子类的典型方法是定义子类,将其原型设置为超类的实例,然后在该实例上定义属性。这么写很不优雅,特别是对于 getters 和 setter 而言。 相反,您可以使用此代码设置原型:</p>
<pre class="brush: js">function superclass() {}
superclass.prototype = {
// 在这里定义方法和属性
};
function subclass() {}
subclass.prototype = Object.create(superclass.prototype, Object.getOwnPropertyDescriptors({
// 在这里定义方法和属性
}));</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-object.getownpropertydescriptors', 'Object.getOwnPropertyDescriptors')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td>Initial definition in ECMAScript 2017.</td>
</tr>
<tr>
<td>{{SpecName('ES2017', '#sec-object.getownpropertydescriptors', 'Object.getOwnPropertyDescriptors')}}</td>
<td>{{Spec2('ES2017')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>{{Compat("javascript.builtins.Object.getOwnPropertyDescriptors")}}</div>
<div> </div>
<h2 id="相关链接">相关链接</h2>
<ul>
<li>{{jsxref("Object.getOwnPropertyDescriptor()")}}</li>
<li>{{jsxref("Object.defineProperty()")}}</li>
<li><a href="https://github.com/tc39/proposal-object-getownpropertydescriptors">Polyfill</a></li>
</ul>
|