6 kyu
Defining getters and setters on an existing class
3,702andrewferk
Description:
Introduction to getter and setter
Javascript classes can declare getter and setters using the following format:
class Engine {
constructor(watts) {
this.watts = watts;
}
get horsepower() {
return this.watts / 745.7;
}
set horsepower(hp) {
this.watts = 745.7 * hp;
}
}
and be used as so:
let bossV8 = new Engine(279637.5);
console.log(bossV8.horsepower); // => 375
bossV8.horsepower = 385;
console.log(bossV8.watts); // => 287094.5
The problem
There is a preloaded class of Person
and it needs a new getter and setter. Person
is defined with a constructor that takes a first name and a last name, and provides a mean to get the first name, last name, and full name. Bellow is the preloaded class:
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
getName() {
return this.firstName + ' ' + this.lastName;
}
}
and can be used as so:
let marcusFenix = new Person('Marcus', 'Fenix');
console.log(marcusFenix.firstName); // => 'Marcus'
console.log(marcusFenix.lastName); // => 'Fenix'
console.log(marcusFenix.getName()); // => 'Marcus Fenix'
- Add a getter of
name
that returns the full name - Add a setter of
name
that modifies first name and last name
After adding the new getter and setter, a Person can be used as so:
let augustusCole = new Person('Augustus', 'Cole');
augustusCole.name = 'Cole Train';
console.log(augustusCole.firstName); // => 'Cole'
console.log(augustusCole.lastName); // => 'Train'
console.log(augustusCole.getName()); // => 'Cole Train'
console.log(augustusCole.name); // => 'Cole Train'
Object-oriented Programming
Fundamentals
Similar Kata:
Stats:
Created | Aug 1, 2015 |
Published | Aug 1, 2015 |
Warriors Trained | 5012 |
Total Skips | 89 |
Total Code Submissions | 12871 |
Total Times Completed | 3702 |
JavaScript Completions | 3702 |
Total Stars | 31 |
% of votes with a positive feedback rating | 89% of 171 |
Total "Very Satisfied" Votes | 140 |
Total "Somewhat Satisfied" Votes | 24 |
Total "Not Satisfied" Votes | 7 |
Total Rank Assessments | 22 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 8 kyu |