Property flags and descriptors Flashcards
Object properties, besides a value, have three special attributes (so-called “flags”):
- writable – if true, can be changed, otherwise it’s read-only.
- enumerable – if true, then listed in loops, otherwise not listed.
- configurable – if true, the property can be deleted and these attributes can be modified, otherwise not.
writable flag
if true, can be changed, otherwise it’s read-only.
enumerable flag
– if true, then listed in loops, otherwise not listed.
configurable flag
– if true, the property can be deleted and these attributes can be modified, otherwise not.
the ___________ method returns a property descriptor for an own property (that is, one directly present on an object and not in the object’s prototype chain) of a given object.
Object.getOwnPropertyDescriptor()
const object1 = { property1: 42 }
const descriptor1 = Object.getOwnPropertyDescriptor(object1, ‘property1’);
console.log(descriptor1.configurable); // expected output:
true
let user = { name: "John" };
Object.defineProperty(user, "name", { // make user name unwritable });
user.name = “Pete”; // returns error
writable: false
let user = { };
Object.defineProperty(user, "name", { value: "Pete", // for new properties need to explicitly list what's true enumerable: true, configurable: true });
alert(user.name); //
user.name = “Alice”; //
Pete
Error
Flag-violating actions are just silently ignored in non-strict.
TRUE / FALSE
true
There’s a method __________ that allows to define many properties at once.
Object.defineProperties
To get all property descriptors at once, we can use the method ___________
Object.getOwnPropertyDescriptors
for (let key in user) {
clone[key] = user[key]
}
THE above copies flags
TRUE / FALSE
FALSE
for (let key in user) {
clone[key] = user[key]
}
a better way of cloning an object with flags is using the method___
Object.defineProperties
Forbids the addition of new properties to the object.
Object.preventExtensions(obj)
Object.preventExtensions(obj)
Forbids the addition of new properties to the object.