aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/enumerability_and_ownership_of_properties/index.html
blob: a20bd79eecd7a040323241aff4277c33217af287 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
---
title: 属性的可枚举性和所有权
slug: Web/JavaScript/Enumerability_and_ownership_of_properties
tags:
  - JavaScript
  - 对象
  - 属性
translation_of: Web/JavaScript/Enumerability_and_ownership_of_properties
---
<div>{{JsSidebar("More")}}</div>

<p>可枚举属性是指那些内部 “可枚举” 标志设置为 <code>true</code> 的属性,对于通过直接的赋值和属性初始化的属性,该标识值默认为即为 <code>true</code>,对于通过 <a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">Object.defineProperty</a> 等定义的属性,该标识值默认为 <code>false</code>。可枚举的属性可以通过 <a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a> 循环进行遍历(除非该属性名是一个 <a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Symbol">Symbol</a>)。属性的所有权是通过判断该属性是否直接属于某个对象决定的,而不是通过原型链继承的。一个对象的所有的属性可以一次性的获取到。有一些内置的方法可以用于判断、迭代/枚举以及获取对象的一个或一组属性,下表对这些方法进行了列举。对于部分不可用的类别,下方的示例代码对获取方法进行了演示。</p>

<div>
<table>
 <caption>属性的可枚举性和所有权 - 内置的判断、访问和迭代方法</caption>
 <tbody>
  <tr>
   <th>作用</th>
   <th>自身对象</th>
   <th>自身对象及其原型链</th>
   <th>仅原型链</th>
  </tr>
  <tr>
   <td>判断</td>
   <td>
    <table>
     <thead>
      <tr>
       <th scope="col">可枚举属性</th>
       <th scope="col">不可枚举属性</th>
       <th scope="col">可枚举属性及不可枚举属性</th>
      </tr>
     </thead>
     <tbody>
      <tr>
       <td><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></code><br>
        <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty">hasOwnProperty</a></code></td>
       <td><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty">hasOwnProperty</a></code> 获取属性后使用 <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></code> 过滤可枚举属性</td>
       <td><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty">hasOwnProperty</a></code></td>
      </tr>
     </tbody>
    </table>
   </td>
   <td>
    <table>
     <thead>
      <tr>
       <th scope="col">可枚举属性</th>
       <th scope="col">不可枚举属性</th>
       <th scope="col">可枚举属性及不可枚举属性</th>
      </tr>
     </thead>
     <tbody>
      <tr>
       <td>需要额外代码实现</td>
       <td>需要额外代码实现</td>
       <td><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/in">in</a></code></td>
      </tr>
     </tbody>
    </table>
   </td>
   <td>需要额外代码实现</td>
  </tr>
  <tr>
   <td>访问</td>
   <td>
    <table>
     <thead>
      <tr>
       <th scope="col">可枚举属性</th>
       <th scope="col">不可枚举属性</th>
       <th scope="col">可枚举属性及不可枚举属性</th>
      </tr>
     </thead>
     <tbody>
      <tr>
       <td><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/keys">Object.keys</a></code><br>
        <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code><br>
        <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a></code></td>
       <td><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a> </code>获取属性后使用 <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></code> 过滤可枚举属性</td>
       <td><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code><br>
        <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a></code></td>
      </tr>
     </tbody>
    </table>
   </td>
   <td>需要额外代码实现</td>
   <td>需要额外代码实现</td>
  </tr>
  <tr>
   <td>迭代</td>
   <td>
    <table>
     <thead>
      <tr>
       <th scope="col">可枚举属性</th>
       <th scope="col">不可枚举属性</th>
       <th scope="col">可枚举属性及不可枚举属性</th>
      </tr>
     </thead>
     <tbody>
      <tr>
       <td><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/keys">Object.keys</a></code><br>
        <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code><br>
        <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a></code></td>
       <td><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a></code> 获取属性后使用 <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></code> 过滤可枚举属性</td>
       <td><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code><br>
        <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a></code></td>
      </tr>
     </tbody>
    </table>
   </td>
   <td>
    <table>
     <thead>
      <tr>
       <th scope="col">可枚举属性</th>
       <th scope="col">不可枚举属性</th>
       <th scope="col">可枚举属性及不可枚举属性</th>
      </tr>
     </thead>
     <tbody>
      <tr>
       <td><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/for...in">for..in</a></code><br>
        (同时会排除 Symbol)</td>
       <td>需要额外代码实现</td>
       <td>需要额外代码实现</td>
      </tr>
     </tbody>
    </table>
   </td>
   <td>需要额外代码实现</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="通过可枚举性和所有权获取对象的属性">通过可枚举性和所有权获取对象的属性</h2>

<p>注:以下实现并非使用于所有情况的最优算法,但可以快捷的展示语言特性。</p>

<ul>
 <li>使用 <code>SimplePropertyRetriever.theGetMethodYouWant(obj).indexOf(prop) &gt; -1</code> 时将发生判断操作。</li>
 <li>使用 <code>SimplePropertyRetriever.theGetMethodYouWant(obj).forEach(function (value, prop) {});</code> 时将发生迭代操作。 (或使用 <code>filter()</code><code>map()</code> 等方法)</li>
</ul>

<pre class="brush: js">var SimplePropertyRetriever = {
    getOwnEnumerables: function(obj) {
        return this._getPropertyNames(obj, true, false, this._enumerable);
         // Or could use for..in filtered with hasOwnProperty or just this: return Object.keys(obj);
    },
    getOwnNonenumerables: function(obj) {
        return this._getPropertyNames(obj, true, false, this._notEnumerable);
    },
    getOwnEnumerablesAndNonenumerables: function(obj) {
        return this._getPropertyNames(obj, true, false, this._enumerableAndNotEnumerable);
        // Or just use: return Object.getOwnPropertyNames(obj);
    },
    getPrototypeEnumerables: function(obj) {
        return this._getPropertyNames(obj, false, true, this._enumerable);
    },
    getPrototypeNonenumerables: function(obj) {
        return this._getPropertyNames(obj, false, true, this._notEnumerable);
    },
    getPrototypeEnumerablesAndNonenumerables: function(obj) {
        return this._getPropertyNames(obj, false, true, this._enumerableAndNotEnumerable);
    },
    getOwnAndPrototypeEnumerables: function(obj) {
        return this._getPropertyNames(obj, true, true, this._enumerable);
        // Or could use unfiltered for..in
    },
    getOwnAndPrototypeNonenumerables: function(obj) {
        return this._getPropertyNames(obj, true, true, this._notEnumerable);
    },
    getOwnAndPrototypeEnumerablesAndNonenumerables: function(obj) {
        return this._getPropertyNames(obj, true, true, this._enumerableAndNotEnumerable);
    },
    // Private static property checker callbacks
    _enumerable: function(obj, prop) {
        return obj.propertyIsEnumerable(prop);
    },
    _notEnumerable: function(obj, prop) {
        return !obj.propertyIsEnumerable(prop);
    },
    _enumerableAndNotEnumerable: function(obj, prop) {
        return true;
    },
    // Inspired by http://stackoverflow.com/a/8024294/271577
    _getPropertyNames: function getAllPropertyNames(obj, iterateSelfBool, iteratePrototypeBool, includePropCb) {
        var props = [];

        do {
            if (iterateSelfBool) {
                Object.getOwnPropertyNames(obj).forEach(function(prop) {
                    if (props.indexOf(prop) === -1 &amp;&amp; includePropCb(obj, prop)) {
                        props.push(prop);
                    }
                });
            }
            if (!iteratePrototypeBool) {
                break;
            }
            iterateSelfBool = true;
        } while (obj = Object.getPrototypeOf(obj));

        return props;
    }
};</pre>

<h2 id="统计表">统计表</h2>

<div>
<table>
 <thead>
  <tr>
   <th scope="row"></th>
   <th scope="col"><code>in</code></th>
   <th scope="col"><code>for..in</code></th>
   <th scope="col"><code>obj.hasOwnProperty</code></th>
   <th scope="col"><code>obj.propertyIsEnumerable</code></th>
   <th scope="col"><code>Object.keys</code></th>
   <th scope="col"><code>Object.getOwnPropertyNames</code></th>
   <th scope="col"><code>Object.getOwnPropertyDescriptors</code></th>
   <th scope="col"><code>Reflect.ownKeys()</code></th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <th scope="row">自身的可枚举属性</th>
   <td>true</td>
   <td>true</td>
   <td>true</td>
   <td>true</td>
   <td>true</td>
   <td>true</td>
   <td>true</td>
   <td>true</td>
  </tr>
  <tr>
   <th scope="row">自身的不可枚举属性</th>
   <td>true</td>
   <td>false</td>
   <td>true</td>
   <td>false</td>
   <td>false</td>
   <td>true</td>
   <td>true</td>
   <td>true</td>
  </tr>
  <tr>
   <th scope="row">自身的Symbol 键</th>
   <td>true</td>
   <td>false</td>
   <td>true</td>
   <td>true</td>
   <td>false</td>
   <td>false</td>
   <td>true</td>
   <td>true</td>
  </tr>
  <tr>
   <th scope="row">继承的可枚举属性</th>
   <td>true</td>
   <td>true</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
  </tr>
  <tr>
   <th scope="row">继承的不可枚举属性</th>
   <td>true</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
  </tr>
  <tr>
   <th scope="row">继承的 Symbol 键</th>
   <td>true</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
   <td>false</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="参见">参见</h2>

<ul>
 <li><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/in">in</a></code></li>
 <li><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/for...in">for..in</a></code></li>
 <li>{{jsxref("Object.hasOwnProperty()")}}</li>
 <li>{{jsxref("Object.propertyIsEnumerable()")}}</li>
 <li>{{jsxref("Object.getOwnPropertyNames()")}}</li>
 <li>{{jsxref("Object.keys()")}}</li>
 <li>{{jsxref("Object.getOwnPropertyDescriptors()")}}</li>
</ul>