Terjemahan ini belum lengkap. Mohon bantu menerjemahkan artikel ini dari Bahasa Inggris.
Metode Object.preventExtensions()
mencegah properti-properti baru untuk ditambahkan pada sebuah obyek (mencegah ekstensi di masa depan pada sebuah obyek).
Sintaks
Object.preventExtensions(obj)
Parameter
obj
- Obyek yang akan dibuat menjadi non-ekstensibel.
Pengembalian nilai
Obyek yang telah dibuat non-ekstensibel.
Deskripsi
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 TypeError
(paling umum, tetapi tidak secara eksklusif, ketika berada di strict mode).
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 __proto__
.
Jika ada cara untuk mengubah suatu obyek ekstensibel untuk satu non-ekstensibel, tidak ada cara untuk melakukan yang sebaliknya di ECMAScript 5 .
Contoh
// 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
Catatan
Pada ES5, jika argumen pada fungsi metode ini adalah bukan sebuah obyek (sebuah tipe data primitif), maka hal itu akan menyebabkan 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
Spesifikasi | Status | Komentar |
---|---|---|
ECMAScript 5.1 (ECMA-262) The definition of 'Object.preventExtensions' in that specification. |
Standard | Definisi awal. Terimplementasi pada JavaScript 1.8.5. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.preventExtensions' in that specification. |
Standard | |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Object.preventExtensions' in that specification. |
Draft |
Browser kompatibilitas
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 6 | 4.0 (2.0) | 9 | 12 | 5.1 |
ES6 behavior for non-object argument | 44 | 35.0 (35.0) | 11 | 31 | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | ? | ? | ? | ? | ? |
ES6 behavior for non-object argument | ? | ? | 35.0 (35.0) | ? | ? | ? |