Property flags and descriptors Flashcards

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
Q

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

A

Object.isFrozen(obj)

26
Q

There are two kinds of properties.

A

data properties.

accessor properties.

27
Q
let obj = {
\_\_\_\_\_\_\_\_\_\_\_\_\_ {
    // getter, the code executed on getting obj.propName
  },
\_\_\_\_\_\_\_\_\_\_\_\_\_ {
    // setter, the code executed on setting obj.propName = value
  }
};
A

get propName()

set propName(value)`

28
Q

let user = {

name: “John”,
surname: “Smith”,

  get fullName() {
    return `${this.name} ${this.surname}`;
  }
};

alert(user.fullName); //

A

John Smith

29
Q

let user = {

name: "John",
surname: "Smith",
    get fullName() {
      return `${this.name} ${this.surname}`;
    },

// fix below

    set fullName(value) {
      [this.name]= value; 

// fix above

A

[this.name] = value.split(“ “);

30
Q

Accessor properties are only accessible with _______

A

get/set

31
Q

Once a property is defined with get prop() or set prop(), it’s an_________ property, not a_______ property any more.

A

accessor

data

32
Q

If there’s a getter – we can read _________, otherwise we can’t.

A

object.prop

33
Q

If there’s a setter – we can set __________ .., otherwise we can’t.

A

object.prop=.

34
Q

a property can be either an accessor or a data property, not both.

TRUE / FALSE

A

true

35
Q

Object.defineProperty({}, ‘prop’, {
get() {
return 1
},

  value: 2
}); 
//
A

// 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
Q

accessor descriptor may have:

A

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
Q
function User(name, age) {
  this.name = name;
  this.age = age;
}

let john = new User(“John”, 25);

alert( john.age ); //

A

25

38
Q

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.

A

under score

39
Q

Note that again, as with $, the use of _ is merely a convention and is not enforced by JavaScript itself.

TRUE / FLASE

A

true