Getters & Setters Flashcards

1
Q

You can create getters and setters in three different ways:

A
  1. with the default method syntax (getter and setter methods),
  2. with the get and set keywords,
  3. with the Object.defineProperty() method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a getter using the default method syntax (getter and setter methods) for

var myCar = { 
  color: "blue",
}
A

getColor: function (){
return this.color;
}

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

Write a getter with the get and set keywords,

var myCar = { 
  color: "blue",
}
A

get: color(){
return this.color
}

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

Write a getter with the Object.defineProperty() method.

var myCar = { 
  color: "blue",
}
A
Object.defineProperty(myCar, "color", {
	get: function() { 
         return this.defColor; 
    },
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What takes in a argument?

Setter or getter

A

setter

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

what uses the return keyword?

A

getter

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

The most important thing to remember about the ___ keyword is that it defines an accessor property, rather than a method.

A

get

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

The biggest advantage of using the get and set keywords is that you can keep data properties ______

A

private.

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

You can use the_____________ method of the global Object to add getters and setters to an already existing object.

A

defineProperty()

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

The static method _________ defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

A

Object.defineProperty()

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

Object.defineProperty(________)

hint: 3 of them

A

obj, prop, descriptor

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

the 6 accessor descriptors

A
get
set
Value
writable
configurable
enumerable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Properties created via Object.defineProperties, Object.defineProperty and Reflect.defineProperty automatically configure to __________

A

writable: false

Read only

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

2 ways to Protect data with getters and setters

A
  1. Bock scoping

2. Function scoping

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

Write a setter using the default method syntax (getter and setter methods) for

var myCar = { 
  color: "blue",
}
A

setColor: function(newColor){
this.color =newColor;
}

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

Write a setter with the get and set keywords,

var myCar = { 
  color: "blue",
}
A
set color (newColor){
this.defColor = newColor;
}
17
Q

Write a setter with the Object.defineProperty() method.

var myCar = { 
  color: "blue",
}
A
Object.defineProperty(myCar, "color", {
	set: function(newColor) {
		this.defColor = newColor;
	},
});