From 40f152ba158e0b8377ea01b4ed22f8d1a9e37b43 Mon Sep 17 00:00:00 2001 From: Alan Price <23743415+alanpaulprice@users.noreply.github.com> Date: Mon, 3 Dec 2018 13:40:09 +0000 Subject: [PATCH] Challenge - ES6: Use getters and setters to Control Access to an Object - Added solution and explanation of private variable naming convention (#24362) * added explanation of private variable naming convention * Added "Note:" to the front of the explanation * Added solution * Moved "Note:" to it's own line and made it bold * fix: moved note block at the bottom --- ...-to-control-access-to-an-object.english.md | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.english.md index fc793cc0d5..45fdd88cd3 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.english.md @@ -9,10 +9,12 @@ challengeType: 1 You can obtain values from an object, and set a value of a property within an object. These are classically called getters and setters. Getter functions are meant to simply return (get) the value of an object's private variable to the user without the user directly accessing the private variable. -Setter functions are meant to modify (set) the value of an object's private variable based on the value passed into the setter function. This change could involve calculations, or even overwriting the previous value completely. +Setter functions are meant to modify (set) the value of an object's private variable based on the value passed into the setter function. This change could involve calculations, or even overwriting the previous value completely.

class Book {
  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
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 (_). The practice itself does not make a variable private. ## Instructions @@ -72,6 +74,27 @@ temp = thermos.temperature; // 26 in C
```js -// solution required +function makeClass() { + "use strict"; + /* Alter code below this line */ + class Thermostat { + constructor(fahrenheit) { + this._tempInCelsius = 5/9 * (fahrenheit - 32); + } + get tempInCelsius(){ + return _tempInCelsius; + } + set tempInCelsius(newTemp){ + this._tempInCelsius = newTemp; + } + } + /* Alter code above this line */ + return Thermostat; +} +const Thermostat = makeClass(); +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 ```