Property flags and descriptors Flashcards

(39 cards)

1
Q

Object properties, besides a value, have three special attributes (so-called “flags”):

A
  1. writable – if true, can be changed, otherwise it’s read-only.
  2. enumerable – if true, then listed in loops, otherwise not listed.
  3. configurable – if true, the property can be deleted and these attributes can be modified, otherwise not.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

writable flag

A

if true, can be changed, otherwise it’s read-only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

enumerable flag

A

– if true, then listed in loops, otherwise not listed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

configurable flag

A

– if true, the property can be deleted and these attributes can be modified, otherwise not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A

Object.getOwnPropertyDescriptor()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
const object1 = {
  property1: 42
}

const descriptor1 = Object.getOwnPropertyDescriptor(object1, ‘property1’);

console.log(descriptor1.configurable);
// expected output:
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
let user = {
  name: "John"
};
Object.defineProperty(user, "name", {
// make user name unwritable 
});

user.name = “Pete”; // returns error

A

writable: false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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”; //

A

Pete

Error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Flag-violating actions are just silently ignored in non-strict.

TRUE / FALSE

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

There’s a method __________ that allows to define many properties at once.

A

Object.defineProperties

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

To get all property descriptors at once, we can use the method ___________

A

Object.getOwnPropertyDescriptors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

for (let key in user) {
clone[key] = user[key]
}

THE above copies flags

TRUE / FALSE

A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

for (let key in user) {
clone[key] = user[key]
}

a better way of cloning an object with flags is using the method___

A

Object.defineProperties

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Forbids the addition of new properties to the object.

A

Object.preventExtensions(obj)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Object.preventExtensions(obj)

A

Forbids the addition of new properties to the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Object.seal(obj)

A

Forbids adding/removing of properties. Sets configurable: false for all existing properties.

17
Q

Forbids adding/removing of properties. Sets configurable: false for all existing properties.

A

Object.seal(obj)

18
Q

Object.freeze(obj)

A

Forbids adding/removing/changing of properties. Sets configurable: false, writable: false for all existing properties. And also there are tests for them:

19
Q

Forbids adding/removing/changing of properties. Sets configurable: false, writable: false for all existing properties. And also there are tests for them:

A

Object.freeze(obj)

20
Q

Object.isExtensible(obj)

A

Returns false if adding properties is forbidden, otherwise true.

21
Q

Returns false if adding properties is forbidden, otherwise true.

A

Object.isExtensible(obj)

22
Q

Object.isSealed(obj)

A

Returns true if adding/removing properties is forbidden, and all existing properties have configurable: false

23
Q

Returns true if adding/removing properties is forbidden, and all existing properties have configurable: false

A

Object.isSealed(obj)

24
Q

Object.isFrozen(obj)

A

Returns true if adding/removing/changing properties is forbidden, and all current properties are configurable: false, writable: false.

25
Returns true if adding/removing/changing properties is forbidden, and all current properties are configurable: false, writable: false.
Object.isFrozen(obj)
26
There are two kinds of properties.
data properties. accessor properties.
27
``` let obj = { _____________ { // getter, the code executed on getting obj.propName }, ``` ``` _____________ { // setter, the code executed on setting obj.propName = value } }; ```
get propName() set propName(value)`
28
let user = { name: "John", surname: "Smith", ``` get fullName() { return `${this.name} ${this.surname}`; } }; ``` alert(user.fullName); //
John Smith
29
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(" ");
30
Accessor properties are only accessible with _______
get/set
31
Once a property is defined with get prop() or set prop(), it’s an_________ property, not a_______ property any more.
accessor data
32
If there’s a getter – we can read _________, otherwise we can’t.
object.prop
33
If there’s a setter – we can set __________ .., otherwise we can’t.
object.prop=.
34
a property can be either an accessor or a data property, not both. TRUE / FALSE
true
35
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:
36
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.
37
``` function User(name, age) { this.name = name; this.age = age; } ``` let john = new User("John", 25); alert( john.age ); //
25
38
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
39
Note that again, as with $, the use of _ is merely a convention and is not enforced by JavaScript itself. TRUE / FLASE
true