From 9341aadb5b2421c3c0526ab93c657590218ce20c Mon Sep 17 00:00:00 2001
From: lasjorg <28780271+lasjorg@users.noreply.github.com>
Date: Sat, 9 Feb 2019 06:47:53 +0100
Subject: [PATCH] Fix/remove property references and update tests (#35109)
* fix(challenges): Update challenge text and assertion test
* fix(challenges): update assert test regex
* fix(challenges): update regex, fix inline comment in code example
* fix: corrected regex
---
...-from-being-modified-externally.english.md | 21 ++++++++++++-------
1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/use-closure-to-protect-properties-within-an-object-from-being-modified-externally.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/use-closure-to-protect-properties-within-an-object-from-being-modified-externally.english.md
index 86fe797eb9..727ba59987 100644
--- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/use-closure-to-protect-properties-within-an-object-from-being-modified-externally.english.md
+++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/use-closure-to-protect-properties-within-an-object-from-being-modified-externally.english.md
@@ -6,12 +6,17 @@ challengeType: 1
## Description
+
In the previous challenge, bird
had a public property name
. It is considered public because it can be accessed and changed outside of bird
's definition.
+
bird.name = "Duffy";
+
Therefore, any part of your code can easily change the name of bird
to any value. Think about things like passwords and bank accounts being easily changeable by any part of your codebase. That could cause a lot of issues.
-The simplest way to make properties private is by creating a variable within the constructor function. This changes the scope of that variable to be within the constructor function versus available globally. This way, the property can only be accessed and changed by methods also within the constructor function.
-function Bird() {
let hatchedEgg = 10; // private property
this.getHatchedEggCount = function() { // publicly available method that a bird object can use
return hatchedEgg;
};
}
let ducky = new Bird();
ducky.getHatchedEggCount(); // returns 10
-Here getHatchedEggCount
is a privileged method, because it has access to the private variable hatchedEgg
. This is possible because hatchedEgg
is declared in the same context as getHatchedEggCount
. In JavaScript, a function always has access to the context in which it was created. This is called closure
.
+
+The simplest way to make this public property private is by creating a variable within the constructor function. This changes the scope of that variable to be within the constructor function versus available globally. This way, the variable can only be accessed and changed by methods also within the constructor function.
+function Bird() {
let hatchedEgg = 10; // private variable
/* publicly available method that a bird object can use */
this.getHatchedEggCount = function() {
return hatchedEgg;
};
}
let ducky = new Bird();
ducky.getHatchedEggCount(); // returns 10
+Here getHachedEggCount
is a privileged method, because it has access to the private variable hatchedEgg
. This is possible because hatchedEgg
is declared in the same context as getHachedEggCount
. In JavaScript, a function always has access to the context in which it was created. This is called closure
.
+
## Instructions
@@ -24,10 +29,12 @@ Change how weight
is declared in the Bird
function so
```yml
tests:
- - text: The weight
property should be a private variable.
- testString: assert(!code.match(/this\.weight/g), 'The weight
property should be a private variable.');
- - text: Your code should create a method in Bird
called getWeight
that returns the weight
.
- testString: assert((new Bird()).getWeight() === 15, 'Your code should create a method in Bird
called getWeight
that returns the weight
.');
+ - text: The weight
property should be a private variable and should be assigned the value of 15
.
+ testString: assert(code.match(/(var|let|const)\s+weight\s*\=\s*15\;?/g), 'The weight
property should be a private variable and should be assigned the value of 15
.');
+ - text: Your code should create a method in Bird
called getWeight
that returns the value of the private variable weight
.
+ testString: assert((new Bird()).getWeight() === 15, 'Your code should create a method in Bird
called getWeight
that returns the value of the private variable weight
.');
+ - text: Your getWeight
function should return the private variable weight
.
+ testString: assert(code.match(/((return\s+)|(\(\s*\)\s*\=\>\s*))weight\;?/g), 'Your getWeight
function should return the private variable weight
.');
```