Property Flags / Descriptors
Object Properties Configuration: Property Flags / Descriptors
What are property attributes/flags that allow special access to an object?
View Answer:
let user = {
name: 'John',
};
let descriptor = Object.getOwnPropertyDescriptor(user, 'name');
console.log(JSON.stringify(descriptor, null, 2));
{
"value": "John",
"writable": true,
"enumerable": true,
"configurable": true
}
Explain the function and syntax of the Object.getOwnPropertyDescriptor method in JavaScript?
View Answer:
Syntax: Object.getOwnPropertyDescriptor(obj, propertyName);
let user = {
name: 'John',
};
let descriptor = Object.getOwnPropertyDescriptor(user, 'name');
console.log(JSON.stringify(descriptor, null, 2));
/* property descriptor:
{
"value": "John",
"writable": true,
"enumerable": true,
"configurable": true
}
*/
Is there a method to define property attributes, writable, enumerable, and configurable in JavaScript?
View Answer:
Syntax: Object.defineProperty(obj, prop, descriptor);
let user = {};
let user = {};
Object.defineProperty(user, 'name', {
value: 'John',
});
let descriptor = Object.getOwnPropertyDescriptor(user, 'name');
console.log(JSON.stringify(descriptor, null, 2));
/*
{
"value": "John",
"writable": false,
"enumerable": false,
"configurable": false
}
*/
You should note that writable, enumerable, and configurable are all set to false, by default, on empty objects. If you do not set the property attributes when using the defineProperty method on empty objects, they return false by default.
When you are creating a method for an object. Is there a way to restrict the enumeration of the newly created object method?
View Answer:
let user = {
name: 'John',
toString() {
return this.name;
},
};
Object.defineProperty(user, 'toString', {
enumerable: false,
});
// Now our custom toString method disappears:
for (let key in user) console.log(key); // returns name, but no toString
We should note that they are all set to false by default when using the defineProperty method on an empty Object.
Is there a way to prevent changes in property flags and their deletion while allowing changes to their value?
View Answer:
let user = {
name: 'John',
};
Object.defineProperty(user, 'name', {
configurable: false,
});
user.name = 'Pete'; // works fine
delete user.name; // Error
Besides the seal() built-in JavaScript method, is there a way to seal an object property?
View Answer:
let user = {
name: 'John',
};
Object.defineProperty(user, 'name', {
writable: false,
configurable: false,
});
// won't be able to change user.name or its flags
// this won't work:
user.name = 'Pete';
delete user.name;
Object.defineProperty(user, 'name', { value: 'Pete' });
If you want to define many properties at once in an object. What built-in JavaScript method can you use?
View Answer:
let obj = {};
Object.defineProperties(obj, {
name: {
value: 'Jane',
writable: true,
},
surname: {
value: 'Doe',
writable: false,
},
// etc. etc.
});
console.log(obj.name); // returns Jane
Explain, the function and syntax of the Object.preventExtension(obj) method in JavaScript?
View Answer:
Syntax: Object.preventExtensions(obj);
const object1 = {};
Object.preventExtensions(object1);
try {
Object.defineProperty(object1, 'property1', {
value: 42,
});
} catch (e) {
console.log(e.message);
}
// expected output: TypeError: Cannot define property property1, object is not extensible
You should note that the attributes of a non-extensible object can still be erased in general. Adding additional attributes to a non-extensible object fails silently or with a TypeError (most commonly, but not exclusively, when strict mode is enabled).
Can you explain the function and syntax of the Object.seal(obj) method in JavaScript?
View Answer:
Syntax: Object.seal(obj);
const object1 = {
property1: 42,
};
Object.seal(object1);
object1.property1 = 33;
console.log(object1.property1);
// expected output: 33
delete object1.property1; // cannot delete when sealed
console.log(object1.property1);
// expected output: 33
Attempting to delete or add properties to a sealed object, convert a data property to an accessor, or vice versa, fails, either silently or by throwing a TypeError (most commonly, although not exclusively, when in strict mode code).
Can you explain the function and syntax of the Object.freeze(obj) method in JavaScript?
View Answer:
Syntax: Object.freeze(obj);
const obj = {
prop: 42,
};
Object.freeze(obj);
obj.prop = 33;
// Throws an error in strict mode
console.log(obj.prop);
// expected output: 42
What is the difference between freeze and seal in JavaScript?
View Answer:
Can you explain the function and syntax of the Object.isExtensible() method in JavaScript?
View Answer:
Syntax: Object.isExtensible();
const object1 = {};
console.log(Object.isExtensible(object1));
// expected output: true
Object.preventExtensions(object1);
console.log(Object.isExtensible(object1));
// expected output: false
Can you explain the function and syntax of the Object.isSealed() method in JavaScript?
View Answer:
Syntax: Object.isSealed(obj);
const object1 = {
property1: 42,
};
console.log(Object.isSealed(object1));
// expected output: false
Object.seal(object1);
console.log(Object.isSealed(object1));
// expected output: true
Can you explain the function and syntax of the Object.isFrozen() method in JavaScript?
View Answer:
Syntax: Object.isFrozen(obj);
const object1 = {
property1: 42,
};
console.log(Object.isFrozen(object1));
// expected output: false
Object.freeze(object1);
console.log(Object.isFrozen(object1));
// expected output: true