aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/object/isextensible/index.html
blob: 249ce52673cc00b0eedc004fd729e3097c45d809 (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
---
title: Object.isExtensible()
slug: Web/JavaScript/Reference/Global_Objects/Object/isExtensible
translation_of: Web/JavaScript/Reference/Global_Objects/Object/isExtensible
---
<div>{{JSRef("Global_Objects", "Object")}}</div>

<h2 id="Summary" name="Summary">概述</h2>

<p><code><strong>Object.isExtensible()</strong></code> 方法判断一个对象是否是可扩展的(是否可以在它上面添加新的属性)。</p>

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

<pre class="syntaxbox"><code>Object.isExtensible(<em>obj</em>)</code></pre>

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

<dl>
 <dt>obj</dt>
 <dd>需要检测的对象</dd>
</dl>

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

<p>    表示给定对象是否可扩展的一个<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Boolean" title="此页面仍未被本地化, 期待您的翻译!"><code>Boolean</code></a> 。</p>

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

<p>默认情况下,对象是可扩展的:即可以为他们添加新的属性。以及它们的 {{jsxref("Object.proto", "__proto__")}}{{deprecated_inline}} 属性可以被更改。{{jsxref("Object.preventExtensions")}}{{jsxref("Object.seal")}}{{jsxref("Object.freeze")}} 方法都可以标记一个对象为不可扩展(non-extensible)。</p>

<h2 id="Examples" name="Examples">例子</h2>

<pre class="brush: js">// 新对象默认是可扩展的.
var empty = {};
Object.isExtensible(empty); // === true

// ...可以变的不可扩展.
Object.preventExtensions(empty);
Object.isExtensible(empty); // === false

// 密封对象是不可扩展的.
var sealed = Object.seal({});
Object.isExtensible(sealed); // === false

// 冻结对象也是不可扩展.
var frozen = Object.freeze({});
Object.isExtensible(frozen); // === false
</pre>

<p> </p>

<h2 id="注意" style="margin-bottom: 20px; line-height: 30px;">注意</h2>

<p>在 ES5 中,如果参数不是一个对象类型,将抛出一个 {{jsxref("TypeError")}} 异常。在 ES6 中, non-object 参数将被视为一个不可扩展的普通对象,因此会返回 false 。</p>

<pre>Object.isExtensible(1);
// TypeError: 1 is not an object (ES5 code)

Object.isExtensible(1);
// false                         (ES6 code)
</pre>

<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</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('ES5.1', '#sec-15.2.3.13', 'Object.isExtensible')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>Initial definition.<br>
    Implemented in JavaScript 1.8.5</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-object.isextensible', 'Object.isExtensible')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

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

<div>{{CompatibilityTable}}</div>

<div>
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Firefox (Gecko)</th>
   <th>Chrome</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>4 (2.0)</td>
   <td>6</td>
   <td>9</td>
   <td>12</td>
   <td>5.1</td>
  </tr>
 </tbody>
</table>
</div>

<div>
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Android</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also" name="See_also" style="margin-bottom: 20px; line-height: 30px;">相关链接</h2>

<ul>
 <li><strong>{{jsxref("Object.preventExtensions")}}</strong></li>
 <li>{{jsxref("Object.seal")}}</li>
 <li>{{jsxref("Object.isSealed")}}</li>
 <li>{{jsxref("Object.freeze")}}</li>
 <li>{{jsxref("Object.isFrozen")}}</li>
</ul>