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.
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.
Returns true if adding/removing/changing properties is forbidden, and all current properties are configurable: false, writable: false.
Object.isFrozen(obj)
There are two kinds of properties.
data properties.
accessor properties.
let obj = { \_\_\_\_\_\_\_\_\_\_\_\_\_ { // getter, the code executed on getting obj.propName },
\_\_\_\_\_\_\_\_\_\_\_\_\_ { // setter, the code executed on setting obj.propName = value } };
get propName()
set propName(value)`
let user = {
name: “John”,
surname: “Smith”,
get fullName() { return `${this.name} ${this.surname}`; } };
alert(user.fullName); //
John Smith
let user = {
name: "John", surname: "Smith",
get fullName() { return `${this.name} ${this.surname}`; },
// fix below
set fullName(value) { [this.name]= value;
// fix above
[this.name] = value.split(“ “);
Accessor properties are only accessible with _______
get/set
Once a property is defined with get prop() or set prop(), it’s an_________ property, not a_______ property any more.
accessor
data
If there’s a getter – we can read _________, otherwise we can’t.
object.prop
If there’s a setter – we can set __________ .., otherwise we can’t.
object.prop=.
a property can be either an accessor or a data property, not both.
TRUE / FALSE
true
Object.defineProperty({}, ‘prop’, {
get() {
return 1
},
value: 2 }); //
// Error: Invalid property descriptor.
A property can be either an accessor or a data property, not both. If we try to supply both get and value in the same descriptor, there will be an error:
accessor descriptor may have:
get – a function without arguments, that works when a property is read,
set – a function with one argument, that is called when the property is set,
enumerable – same as for data properties,
configurable – same as for data properties.
function User(name, age) { this.name = name; this.age = age; }
let john = new User(“John”, 25);
alert( john.age ); //
25
A convention has also developed regarding the use of _________ which is frequently used to preface a name of an object’s property or method that is private.
under score
Note that again, as with $, the use of _ is merely a convention and is not enforced by JavaScript itself.
TRUE / FLASE
true