Getters & Setters Flashcards
You can create getters and setters in three different ways:
- with the default method syntax (getter and setter methods),
- with the get and set keywords,
- with the Object.defineProperty() method.
Write a getter using the default method syntax (getter and setter methods) for
var myCar = { color: "blue", }
getColor: function (){
return this.color;
}
Write a getter with the get and set keywords,
var myCar = { color: "blue", }
get: color(){
return this.color
}
Write a getter with the Object.defineProperty() method.
var myCar = { color: "blue", }
Object.defineProperty(myCar, "color", { get: function() { return this.defColor; }, });
What takes in a argument?
Setter or getter
setter
what uses the return keyword?
getter
The most important thing to remember about the ___ keyword is that it defines an accessor property, rather than a method.
get
The biggest advantage of using the get and set keywords is that you can keep data properties ______
private.
You can use the_____________ method of the global Object to add getters and setters to an already existing object.
defineProperty()
The static method _________ defines a new property directly on an object, or modifies an existing property on an object, and returns the object.
Object.defineProperty()
Object.defineProperty(________)
hint: 3 of them
obj, prop, descriptor
the 6 accessor descriptors
get set Value writable configurable enumerable
Properties created via Object.defineProperties, Object.defineProperty and Reflect.defineProperty automatically configure to __________
writable: false
Read only
2 ways to Protect data with getters and setters
- Bock scoping
2. Function scoping
Write a setter using the default method syntax (getter and setter methods) for
var myCar = { color: "blue", }
setColor: function(newColor){
this.color =newColor;
}