aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/enumerability_and_ownership_of_properties/index.html
blob: 500527c489d3a5972fb708190b95b75d9ce0ab4f (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
---
title: プロパティの列挙可能性と所有権
slug: Web/JavaScript/Enumerability_and_ownership_of_properties
tags:
  - Guide
  - JavaScript
translation_of: Web/JavaScript/Enumerability_and_ownership_of_properties
---
<div>{{JsSidebar("More")}}</div>

<p>列挙可能プロパティは、内部の<ruby>列挙可能<rp></rp><rt>enumerable</rt><rp></rp></ruby>フラグが true に設定されているプロパティです。これは、単純な代入や初期化で作成されたプロパティのデフォルトです (<a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">Object.defineProperty</a> で追加したプロパティはデフォルトで列挙可能性が false になります)。プロパティのキーが <a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Symbol">Symbol</a> でない限り、列挙可能なプロパティは <a href="/ja/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a> ループにの対象になります。プロパティの所有権は、プロパティがプロトタイプチェーンではなく、オブジェクトに直接属しているかどうかによって決まります。オブジェクトのプロパティはまとめて取り扱うこともでき、プロパティを検出、反復、列挙、取得するための多くの組み込み機能があります。以下に、使用可能なチャートと不足しているカテゴリを取得する方法を示すサンプルコードを示します。</p>

<div style="overflow: auto; width: 100%;">
<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>
        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></code></p>

        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty">hasOwnProperty</a></code></p>
       </td>
       <td>
        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty">hasOwnProperty</a></code><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></code> を使用して列挙可能なものを除外するようにフィルターリング</p>
       </td>
       <td><code><a href="/ja/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="/ja/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>
        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/keys">Object.keys</a></code></p>

        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code></p>

        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a></code></p>
       </td>
       <td><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code>, <code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a></code><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></code> を使用して列挙可能なものを除外するようにフィルターリング</td>
       <td>
        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code></p>

        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a></code></p>
       </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>
        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/keys">Object.keys</a></code></p>

        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code> </p>

        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a></code></p>
       </td>
       <td><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code>, <code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a></code><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></code> を使用して列挙可能なものを除外するようにフィルターリング</td>
       <td>
        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></code></p>

        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols">getOwnPropertySymbols</a></code></p>
       </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>
        <p><code><a href="/ja/docs/Web/JavaScript/Reference/Statements/for...in">for..in</a></code></p>

        <p>(symbol を除く)</p>
       </td>
       <td>追加のコードが必要</td>
       <td>追加のコードが必要</td>
      </tr>
     </tbody>
    </table>
   </td>
   <td>追加のコードが必要</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Obtaining_properties_by_enumerabilityownership" name="Obtaining_properties_by_enumerabilityownership">列挙可能性/所有権によるプロパティの取得</h2>

<p>以下に示すのは全てのケースで最も効率的なアルゴリズムではなく、簡潔なデモであることに注意してください。</p>

<ul>
 <li>検出は以下の方法で行うことができます。<br>
<code>SimplePropertyRetriever.使いたい get メソッド(obj).indexOf(prop) &gt; -1</code></li>
 <li>反復は以下の方法で行うことができます。<br>
<code>SimplePropertyRetriever.使いたい get メソッド(obj).forEach(function (value, prop) {});</code> (または <code> filter()</code>, <code>map()</code> などを使う)</li>
</ul>

<pre class="brush: js notranslate">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="Detection_Table" name="Detection_Table">検出テーブル</h2>

<div style="overflow: auto; width: 100%;">
<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">Symbols キー</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">継承された Symbols キー</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="See_also" name="See_also">関連情報</h2>

<ul>
 <li><code><a href="/ja/docs/Web/JavaScript/Reference/Operators/in">in</a></code></li>
 <li><code><a href="/ja/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>