* feat: removed IIFE and added solution * feat: updated challenges seed, test and solution * style: removed semicolon * feat: updated seed and solution * feat: updated challenges seed and solution * feat: updated test, seed and solution * fix: added seed code to solution * fix: removed function and added solution * fix: removed makeClass function and fixed solution * style: removed excessive semicolons * Fixed spacing for note in instructions section * fix: removed assert messages and used const * fix: regex fails correctly now
3.9 KiB
3.9 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d7b8c367417b2b2512b54 | Use getters and setters to Control Access to an Object | 1 |
Description
class Book {Notice the syntax we are using to invoke the getter and setter - as if they are not even functions. Getters and setters are important, because they hide internal implementation details. Note: It is a convention to precede the name of a private variable with an underscore (
constructor(author) {
this._author = author;
}
// getter
get writer(){
return this._author;
}
// setter
set writer(updatedAuthor){
this._author = updatedAuthor;
}
}
const lol = new Book('anonymous');
console.log(lol.writer); // anonymous
lol.writer = 'wut';
console.log(lol.writer); // wut
_
). The practice itself does not make a variable private.
Instructions
class
keyword to create a Thermostat class. The constructor accepts Fahrenheit temperature.
Now create getter
and setter
in the class, to obtain the temperature in Celsius scale.
Remember that C = 5/9 * (F - 32)
and F = C * 9.0 / 5 + 32
, where F is the value of temperature in Fahrenheit scale, and C is the value of the same temperature in Celsius scale
Note:When you implement this, you would be tracking the temperature inside the class in one scale - either Fahrenheit or Celsius. This is the power of getter or setter - you are creating an API for another user, who would get the correct result, no matter which one you track. In other words, you are abstracting implementation details from the consumer.
Tests
tests:
- text: <code>Thermostat</code> should be a <code>class</code> with a defined <code>constructor</code> method.
testString: assert(typeof Thermostat === 'function' && typeof Thermostat.constructor === 'function');
- text: <code>class</code> keyword should be used.
testString: assert(code.match(/class/g));
- text: <code>Thermostat</code> should be able to be instantiated.
testString: assert((() => {const t = new Thermostat(32);return typeof t === 'object' && t.temperature === 0;})());
Challenge Seed
/* Alter code below this line */
/* Alter code above this line */
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C
Solution
/* Alter code below this line */
class Thermostat {
constructor(fahrenheit) {
this._tempInCelsius = 5/9 * (fahrenheit - 32);
}
get temperature(){
return this._tempInCelsius;
}
set temperature(newTemp){
this._tempInCelsius = newTemp;
}
}
/* Alter code above this line */
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C