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
|
---
title: Object.preventExtensions()
slug: Web/JavaScript/Reference/Global_Objects/Object/preventExtensions
translation_of: Web/JavaScript/Reference/Global_Objects/Object/preventExtensions
---
<div>{{JSRef}}</div>
<p>Metode <code><strong>Object.preventExtensions()</strong></code> mencegah properti-properti baru untuk ditambahkan pada sebuah obyek (mencegah ekstensi di masa depan pada sebuah obyek).</p>
<h2 id="Sintaks">Sintaks</h2>
<pre class="syntaxbox"><code>Object.preventExtensions(<var>obj</var>)</code></pre>
<h3 id="Parameter">Parameter</h3>
<dl>
<dt><code>obj</code></dt>
<dd>Obyek yang akan dibuat menjadi non-ekstensibel.</dd>
</dl>
<h3 id="Pengembalian_nilai">Pengembalian nilai</h3>
<p>Obyek yang telah dibuat non-ekstensibel.</p>
<h2 id="Deskripsi">Deskripsi</h2>
<p> </p>
<p>Sebuah obyek disebut ekstensibel jika properti baru dapat ditambahkan ke dalamnya . <code>Object.preventExtensions()</code> menandai suatu obyek untuk menjadi tidak ekstensibel, sehingga tidak akan pernah memiliki properti diluar properti-properti yang dimiliki sebelum ditandai sebagai non-ekstensibel. Perhatikan bahwa properti-properti obyek non-ekstensibel, secara umum, masih dapat <em>dihapus</em>. Mencoba untuk menambah properti baru ke obyek non-ekstensibel akan gagal, baik secara diam-diam atau dengan melemparkan {{jsxref("TypeError")}} (paling umum, tetapi tidak secara eksklusif, ketika berada di {{jsxref("Functions_and_function_scope/Strict_mode", "strict mode", "", 1)}}).</p>
<p><code>Object.preventExtensions()</code> hanya mencegah penambahan properti sendiri . Sifat masih bisa ditambahkan ke prototipe obyek. Namun, memanggil <code>Object.preventExtensions()</code> pada obyek juga akan mencegah ekstensi / penambahan properti {{jsxref("Object.proto", "__proto__")}} {{deprecated_inline}}.</p>
<p>Jika ada cara untuk mengubah suatu obyek ekstensibel untuk satu non-ekstensibel, tidak ada cara untuk melakukan yang sebaliknya di ECMAScript 5 .</p>
<h2 id="Contoh">Contoh</h2>
<pre class="brush: js">// Object.preventExtensions returns the object being made non-extensible.
var obj = {};
var obj2 = Object.preventExtensions(obj);
obj === obj2; // true
// Objects are extensible by default.
var empty = {};
Object.isExtensible(empty); // === true
// ...but that can be changed.
Object.preventExtensions(empty);
Object.isExtensible(empty); // === false
// Object.defineProperty throws when adding a new property to a non-extensible object.
var nonExtensible = { removable: true };
Object.preventExtensions(nonExtensible);
Object.defineProperty(nonExtensible, 'new', { value: 8675309 }); // throws a TypeError
// In strict mode, attempting to add new properties to a non-extensible object throws a 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="Catatan">Catatan</h2>
<p>Pada ES5, jika argumen pada fungsi metode ini adalah bukan sebuah obyek (sebuah tipe data primitif), maka hal itu akan menyebabkan {{jsxref("TypeError")}}. Pada ES6, sebuah argumen non-obyek akan diperlakukan seperti obyek tersebut obyek biasa yang non-ekstensibel, dan langsung mengembalikan nilai. </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="Spesifikasi">Spesifikasi</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spesifikasi</th>
<th scope="col">Status</th>
<th scope="col">Komentar</th>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.2.3.10', 'Object.preventExtensions')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td>Definisi awal. Terimplementasi pada JavaScript 1.8.5.</td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-object.preventextensions', 'Object.preventExtensions')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-object.preventextensions', 'Object.preventExtensions')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_kompatibilitas">Browser kompatibilitas</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatChrome("6")}}</td>
<td>{{CompatGeckoDesktop("2.0")}}</td>
<td>{{CompatIE("9")}}</td>
<td>{{CompatOpera("12")}}</td>
<td>{{CompatSafari("5.1")}}</td>
</tr>
<tr>
<td>ES6 behavior for non-object argument</td>
<td>{{CompatChrome("44")}}</td>
<td>{{CompatGeckoDesktop("35.0")}}</td>
<td>{{CompatIE("11")}}</td>
<td>{{CompatOpera("31")}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</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>
<td>{{CompatUnknown}}</td>
</tr>
<tr>
<td>ES6 behavior for non-object argument</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatGeckoMobile("35.0")}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also">See also</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>
|