6 kyu

Defining getters and setters on an existing class

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

More By Author:

Check out these other kata created by andrewferk

Stats:

CreatedAug 1, 2015
PublishedAug 1, 2015
Warriors Trained5012
Total Skips89
Total Code Submissions12871
Total Times Completed3702
JavaScript Completions3702
Total Stars31
% of votes with a positive feedback rating89% of 171
Total "Very Satisfied" Votes140
Total "Somewhat Satisfied" Votes24
Total "Not Satisfied" Votes7
Total Rank Assessments22
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • andrewferk Avatar
  • JohanWiltink Avatar
  • farhanaditya Avatar
Ad