--- title: Object.preventExtensions() slug: Web/JavaScript/Reference/Global_Objects/Object/preventExtensions translation_of: Web/JavaScript/Reference/Global_Objects/Object/preventExtensions ---
Metode Object.preventExtensions() mencegah properti-properti baru untuk ditambahkan pada sebuah obyek (mencegah ekstensi di masa depan pada sebuah obyek).
Object.preventExtensions(obj)
objObyek yang telah dibuat non-ekstensibel.
Sebuah obyek disebut ekstensibel jika properti baru dapat ditambahkan ke dalamnya . Object.preventExtensions() 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 dihapus. 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)}}).
Object.preventExtensions() hanya mencegah penambahan properti sendiri . Sifat masih bisa ditambahkan ke prototipe obyek. Namun, memanggil Object.preventExtensions() pada obyek juga akan mencegah ekstensi / penambahan properti {{jsxref("Object.proto", "__proto__")}} {{deprecated_inline}}.
Jika ada cara untuk mengubah suatu obyek ekstensibel untuk satu non-ekstensibel, tidak ada cara untuk melakukan yang sebaliknya di ECMAScript 5 .
// 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
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.
Object.preventExtensions(1); // TypeError: 1 is not an object (ES5 code) Object.preventExtensions(1); // 1 (ES6 code)
| Spesifikasi | Status | Komentar |
|---|---|---|
| {{SpecName('ES5.1', '#sec-15.2.3.10', 'Object.preventExtensions')}} | {{Spec2('ES5.1')}} | Definisi awal. Terimplementasi pada JavaScript 1.8.5. |
| {{SpecName('ES6', '#sec-object.preventextensions', 'Object.preventExtensions')}} | {{Spec2('ES6')}} | |
| {{SpecName('ESDraft', '#sec-object.preventextensions', 'Object.preventExtensions')}} | {{Spec2('ESDraft')}} |
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | {{CompatChrome("6")}} | {{CompatGeckoDesktop("2.0")}} | {{CompatIE("9")}} | {{CompatOpera("12")}} | {{CompatSafari("5.1")}} |
| ES6 behavior for non-object argument | {{CompatChrome("44")}} | {{CompatGeckoDesktop("35.0")}} | {{CompatIE("11")}} | {{CompatOpera("31")}} | {{CompatUnknown}} |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
| ES6 behavior for non-object argument | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatGeckoMobile("35.0")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |