Created
June 13, 2018 11:53
-
-
Save ernestohegi/caf2093fe2f9e5dcab52f5499a8c1b59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Testing Object.freeze(). | |
const list = { numbers: [1, 2, 3] }; | |
console.log('adding 4 to the unfreezed list of numbers'); | |
list.numbers.push(4); | |
console.log('unfreezed', list); | |
Object.freeze(list); | |
// Number added to the list [1, 2, 3, 4, 5]. | |
console.log('adding 5 to the freezed list of numbers'); | |
list.numbers.push(5); | |
// New property not added because of the freeze method. | |
console.log('adding letters to the freezed list'); | |
list['letters'] = ['a', 'b', 'c']; | |
console.log('freezed', list); | |
const newList = Object.assign({}, list); | |
console.log('adding letters to the new cloned unfreezed list'); | |
newList['letters'] = ['a', 'b', 'c']; | |
console.log('unfreezed', newList); | |
Object.freeze(newList['letters']); | |
// New property not added because of the freeze method. Error thrown. | |
console.log('adding d to the freezed list of letters'); | |
try { | |
newList.letters.push('d'); | |
} catch (error) { | |
console.log(error.message); | |
} | |
console.log('freezed', newList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment