Definition: Getters and setters are used for defining computed properties. A computed property is one that uses a function to get or set an object value.
var person = {
name: "Beyonce",
get name() {
return this.name;
},
set personName(val) {
this.name = val;
}
}
Then in the console:
person.name = "Mrs. Carter"
var bestArtistEver = person.name
console.log(bestArtistEver);
class Person {
constructor(name) {
this._hello = name;
}
get personName() {
return this._hello;
}
set personName (newName) {
this._name = newName;
}
}
Then in the console:
let adele = new Person("Adele")
adele.personName
adele.personName = "A-Dawg"
adele.personName
Definition: Template literals are string literals that can stretch across multiple lines and include interpolated expressions.
Template Strings use back-ticks rather than single or double quotes.
They help make the following easier:
- String interpolation
- String concatenation
- Multiline strings
- String substitution
Multiline strings in ES5:
var teacher = "Andy \n" + "is on a higher level"
Multiline strings in ES6:
var teacher = `Andy
is on a higher level`
String concatenation:
var person = "Andy Kim";
console.log('my name is ' + person + '!');
Better string concatenation:
var person = "Andy Kim";
console.log(`My name is ${person}!`);