1) Property Flags & Descriptors

JavaScript’dagi har bir xossa (property) “yashirin” atributlarga ega:

Oddiy tayinlash (masalan obj.a = 1) orqali yaratilgan xossalarning uchalasi ham default true bo‘ladi.

Object.defineProperty orqali berilsa, ko‘rsatilmagan bayroqlar default false deb olinadi.

Eslatma: Descriptor’lar ikki turga bo‘linadi:

1.2. Ko‘rish: Object.getOwnPropertyDescriptor

const user = { name: 'Ali' };
console.log(Object.getOwnPropertyDescriptor(user, 'name'));
/*
{
  value: 'Ali',
  writable: true,
  enumerable: true,
  configurable: true
}
*/

Barcha xossalarning descriptor’ini birdan ko‘rish:

console.log(Object.getOwnPropertyDescriptors(user));

1.3. O‘zgartirish: Object.defineProperty / Object.defineProperties

const obj = {};
Object.defineProperty(obj, 'id', {
  value: 1001,
  writable: false,      // o‘zgartirib bo‘lmaydi
  enumerable: false,    // looplarda ko‘rinmaydi
  configurable: false   // o‘chirib/descriptorni o‘zgartirib bo‘lmaydi
});

console.log(obj.id);          // 1001
obj.id = 2000;                // strict bo‘lmasa — jim, strict’da TypeError
console.log(Object.keys(obj)); // [] — enumerable:false bo‘lgani uchun ko‘rinmaydi

Bir nechta xossani birvarakayiga:

Object.defineProperties(obj, {
  createdAt: { value: Date.now(), writable: false, enumerable: true, configurable: false },
  note:      { value: 'readonly', writable: false, enumerable: true, configurable: true }
});

1.4. Muhim qoidalar (configurable/writable)

1.5. Accessor descriptor (getter/setter)