Property flags and descriptors Flashcards
(39 cards)
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.
Object.seal(obj)
Forbids adding/removing of properties. Sets configurable: false for all existing properties.
Forbids adding/removing of properties. Sets configurable: false for all existing properties.
Object.seal(obj)
Object.freeze(obj)
Forbids adding/removing/changing of properties. Sets configurable: false, writable: false for all existing properties. And also there are tests for them:
Forbids adding/removing/changing of properties. Sets configurable: false, writable: false for all existing properties. And also there are tests for them:
Object.freeze(obj)
Object.isExtensible(obj)
Returns false if adding properties is forbidden, otherwise true.
Returns false if adding properties is forbidden, otherwise true.
Object.isExtensible(obj)
Object.isSealed(obj)
Returns true if adding/removing properties is forbidden, and all existing properties have configurable: false
Returns true if adding/removing properties is forbidden, and all existing properties have configurable: false
Object.isSealed(obj)
Object.isFrozen(obj)
Returns true if adding/removing/changing properties is forbidden, and all current properties are configurable: false, writable: false.