aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/object/getownpropertydescriptor/index.html
blob: 23450e809f90aaf703fce227479b9f4c885c285a (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
title: Object.getOwnPropertyDescriptor()
slug: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor
tags:
  - JavaScript
  - Object
  - 对象
  - 方法
translation_of: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor
---
<div>{{JSRef}}</div>

<p><strong><code>Object.getOwnPropertyDescriptor()</code></strong> 方法返回指定对象上一个自有属性对应的属性描述符。(自有属性指的是直接赋予该对象的属性,不需要从原型链上进行查找的属性)</p>

<p>{{EmbedInteractiveExample("pages/js/object-getownpropertydescriptor.html")}}</p>

<h2 id="Syntax">语法</h2>

<pre class="syntaxbox">Object.getOwnPropertyDescriptor(<em>obj</em>, <em>prop</em>)</pre>

<h3 id="Parameters">参数</h3>

<dl>
 <dt><code>obj</code></dt>
 <dd>需要查找的目标对象</dd>
 <dt><code>prop</code></dt>
 <dd>目标对象内属性名称</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>如果指定的属性存在于对象上,则返回其属性描述符对象(property descriptor),否则返回 {{jsxref("undefined")}}</p>

<h2 id="Description">描述</h2>

<p>该方法允许对一个属性的描述进行检索。在 Javascript 中, <dfn>属性</dfn> 由一个字符串类型的“名字”(name)和一个“属性描述符”(property descriptor)对象构成。更多关于属性描述符类型以及他们属性的信息可以查看:{{jsxref("Object.defineProperty")}}.</p>

<p>一个属性描述符是一个记录,由下面属性当中的某些组成的:</p>

<dl>
 <dt><code><strong>value</strong></code></dt>
 <dd>该属性的值(仅针对数据属性描述符有效)</dd>
 <dt><code><strong>writable</strong></code></dt>
 <dd><code>当且仅当属性的值可以被改变时为true。(仅针对数据属性描述有效)</code></dd>
 <dt><code><strong>get</strong></code></dt>
 <dd>获取该属性的访问器函数(getter)。如果没有访问器, 该值为undefined。(仅针对包含访问器或设置器的属性描述有效)</dd>
 <dt><code><strong>set</strong></code></dt>
 <dd>获取该属性的设置器函数(setter)。 如果没有设置器, 该值为undefined。(仅针对包含访问器或设置器的属性描述有效)</dd>
 <dt><code><strong>configurable</strong></code></dt>
 <dd><code>当且仅当指定对象的属性描述可以被改变或者属性可被删除时,为true。</code></dd>
 <dt><code><strong>enumerable</strong></code></dt>
 <dd>当且仅当指定对象的属性可以被枚举出时,为 <code>true</code></dd>
</dl>

<h2 id="Examples">示例</h2>

<pre class="brush: js">var o, d;

o = { get foo() { return 17; } };
d = Object.getOwnPropertyDescriptor(o, "foo");
// d {
//   configurable: true,
//   enumerable: true,
//   get: /*the getter function*/,
//   set: undefined
// }

o = { bar: 42 };
d = Object.getOwnPropertyDescriptor(o, "bar");
// d {
//   configurable: true,
//   enumerable: true,
//   value: 42,
//   writable: true
// }

o = {};
Object.defineProperty(o, "baz", {
  value: 8675309,
  writable: false,
  enumerable: false
});
d = Object.getOwnPropertyDescriptor(o, "baz");
// d {
//   value: 8675309,
//   writable: false,
//   enumerable: false,
//   configurable: false
// }</pre>

<h2 id="注意事项">注意事项</h2>

<p>在 ES5 中,如果该方法的第一个参数不是对象(而是原始类型),那么就会产生出现 {{jsxref("TypeError")}}。而在 ES2015,第一个的参数不是对象的话就会被强制转换为对象。</p>

<pre class="brush: js">Object.getOwnPropertyDescriptor('foo', 0);
// 类型错误: "foo" 不是一个对象  // ES5 code

Object.getOwnPropertyDescriptor('foo', 0);
// Object returned by ES2015 code: {
//   configurable: false,
//   enumerable: true,
//   value: "f",
//   writable: false
// }</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">批注</th>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.2.3.3', 'Object.getOwnPropertyDescriptor')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>Initial definition.<br>
    Implemented in JavaScript 1.8.5</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-object.getownpropertydescriptor', 'Object.getOwnPropertyDescriptor')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-object.getownpropertydescriptor', 'Object.getOwnPropertyDescriptor')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>{{Compat("javascript.builtins.Object.getOwnPropertyDescriptor")}}</div>



<div></div>

<div><strong>相关链接</strong> </div>

<ul>
 <li>{{jsxref("Object.defineProperty()")}}</li>
 <li>{{jsxref("Reflect.getOwnPropertyDescriptor()")}}</li>
</ul>