blob: 2a06e61d0c14efa1051b2ac514d6ead78f4a9ebb (
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
|
---
title: Object.preventExtensions()
slug: Web/JavaScript/Reference/Global_Objects/Object/preventExtensions
translation_of: Web/JavaScript/Reference/Global_Objects/Object/preventExtensions
---
<div>{{JSRef}}</div>
<p><code><strong>Object.preventExtensions()</strong></code> 用來避免物件被新增新的屬性。</p>
<h2 id="語法">語法</h2>
<pre class="syntaxbox"><code>Object.preventExtensions(<var>obj</var>)</code></pre>
<h3 id="參數">參數</h3>
<dl>
<dt><code>obj</code></dt>
<dd>要被用作無法擴充的物件。</dd>
</dl>
<h2 id="描述">描述</h2>
<p>物件如果可以被增加新的屬性,我們稱它可以被擴充(extensible)。<code>Object.preventExtensions()</code> 標註物件使它無法被擴充,所以在它被標註為無法擴充當下,它將無法再增加新的屬性。不過注意一點,在一般狀況下,被標註為無法擴充的物件,其屬性仍可被刪除(<em>deleted</em>)。嘗試去增加屬性將會導致失敗,可能會沒有結果產生,或是傳回一個 {{jsxref("TypeError")}} (最常見,但並不是一定,當在{{jsxref("Functions_and_function_scope/Strict_mode", "strict mode", "", 1)}})。</p>
<p><code>Object.preventExtensions() 只有避免物件被增加屬性,屬性仍可以被增加至 </code>object prototype 。不過,呼叫 <code>Object.preventExtensions() 使用在物件上,就可以使其 </code>{{jsxref("Object.proto", "__proto__")}} {{deprecated_inline}} 屬性無法被擴充。</p>
<p>如果能把可擴充物件,轉成無法擴充物件,在 ECMAScript 5 規範中,它並沒有任何方法轉回來。</p>
<h2 id="範例">範例</h2>
<pre class="brush: js">// Object.preventExtensions 傳回一個被無法擴充的物件
var obj = {};
var obj2 = Object.preventExtensions(obj);
obj === obj2; // true
// 預設下,物件可以被擴充
var empty = {};
Object.isExtensible(empty); // === true
// ...但是以下情況之後,無法再被變更。
Object.preventExtensions(empty);
Object.isExtensible(empty); // === false
// Object.defineProperty throws 當為無法擴充的物件增加屬性
var nonExtensible = { removable: true };
Object.preventExtensions(nonExtensible);
Object.defineProperty(nonExtensible, 'new', { value: 8675309 }); // throws a TypeError
// 在 strict mode 中,嘗試去新增屬性給無法擴充物件,將 throws 出一個 TypeError。
function fail() {
'use strict';
nonExtensible.newProperty = 'FAIL'; // throws a TypeError
}
fail();
// EXTENSION (only works in engines supporting __proto__
// (which is deprecated. Use Object.getPrototypeOf instead)):
// A non-extensible object's prototype is immutable.
var fixed = Object.preventExtensions({});
fixed.__proto__ = { oh: 'hai' }; // throws a TypeError
</pre>
<h2 id="筆記">筆記</h2>
<p>在ES5中,如果給祝個方法的參數為非物件,它將造成一個 {{jsxref("TypeError")}} 。不過在 ES6 中,非物件參數會被正常處理。另外,如果它原本就是個無法擴充物件,就只是回傳本身。</p>
<pre class="brush: js">Object.preventExtensions(1);
// TypeError: 1 is not an object (ES5 code)
Object.preventExtensions(1);
// 1 (ES6 code)
</pre>
<h2 id="規範">規範</h2>
{{Specifications}}
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
{{Compat}}
<h2 id="閱讀更多">閱讀更多</h2>
<ul>
<li>{{jsxref("Object.isExtensible()")}}</li>
<li>{{jsxref("Object.seal()")}}</li>
<li>{{jsxref("Object.isSealed()")}}</li>
<li>{{jsxref("Object.freeze()")}}</li>
<li>{{jsxref("Object.isFrozen()")}}</li>
<li>{{jsxref("Reflect.preventExtensions()")}}</li>
</ul>
|