[Guide] ES6: Prevent object mutation; fixes and enhancements (#34649)
* [Guide] ES6: Prevent object mutation; fixes and enhancements - Remove phantom image - Remove notes for contributors - Change explanation and hints. - Links to markdown * Changed as requested Changed titles as requested by @randelldawson. Note: I'm just used to [MDN](https://developer.mozilla.org/en-US/docs/MDN/Contribute/Guidelines/Writing_style_guide#Page_titles) and other language styles and though it was clearer. Is it ok now for this guide? * fix: removed repl.it link
This commit is contained in:
committed by
Randell Dawson
parent
992c39f7a3
commit
97a74b68e8
@ -1,17 +1,15 @@
|
|||||||
---
|
---
|
||||||
title: Prevent Object Mutation
|
title: Prevent Object Mutation
|
||||||
---
|
---
|
||||||

|
|
||||||
|
|
||||||
 Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code 
|
 Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code 
|
||||||
|
|
||||||
### Problem Explanation:
|
### Problem Explanation:
|
||||||
|
|
||||||
We need to prevent `MATH_CONSTANTS` value from changing.
|
_You need to freeze the `MATH_CONSTANTS` object so that no one is able to alter the value of `PI`, add, or delete properties ._
|
||||||
|
|
||||||
##  Hint: 1
|
##  Hint: 1
|
||||||
|
|
||||||
* Use Object.freeze(obj) to prevent object from being changed.
|
* _Use `Object.freeze()` to prevent mathematical constants from changing._
|
||||||
|
|
||||||
> _try to solve the problem now_
|
> _try to solve the problem now_
|
||||||
|
|
||||||
@ -19,9 +17,9 @@ We need to prevent `MATH_CONSTANTS` value from changing.
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Solution ahead!**
|
**Solution Ahead!**
|
||||||
|
|
||||||
##  Basic Code Solution:
|
##  Basic code solution:
|
||||||
```javascript
|
```javascript
|
||||||
function freezeObj() {
|
function freezeObj() {
|
||||||
"use strict";
|
"use strict";
|
||||||
@ -41,21 +39,10 @@ We need to prevent `MATH_CONSTANTS` value from changing.
|
|||||||
|
|
||||||
const PI = freezeObj();
|
const PI = freezeObj();
|
||||||
```
|
```
|
||||||
 <a href='https://codepen.io/dylantyates/pen/OwVxYB' target='_blank' rel='nofollow'>Run Code</a>
|
|
||||||
|
|
||||||
# Code Explanation:
|
# Code Explanation:
|
||||||
|
|
||||||
By using Object.freeze() on `MATH_CONSTANTS` we can avoid manipulating it.
|
By using Object.freeze() on `MATH_CONSTANTS` we can avoid manipulating it.
|
||||||
|
|
||||||
#### Relevant Links
|
|
||||||
|
|
||||||
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze' target='_blank' rel='nofollow'>Object.freeze()</a>
|
### Resources
|
||||||
|
- ["Object.freeze()" - *MDN Javascript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)
|
||||||
##  NOTES FOR CONTRIBUTIONS:
|
|
||||||
|
|
||||||
*  **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution.
|
|
||||||
* Add an explanation of your solution.
|
|
||||||
* Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**. 
|
|
||||||
* Please add your username only if you have added any **relevant main contents**. ( **_DO NOT_** _remove any existing usernames_)
|
|
||||||
|
|
||||||
> See  <a href='http://forum.freecodecamp.com/t/algorithm-article-template/14272' target='_blank' rel='nofollow'>**`Wiki Challenge Solution Template`**</a> for reference.
|
|
||||||
|
Reference in New Issue
Block a user