Created
August 14, 2025 00:24
-
-
Save HenriqueSilverio/ecbddf532a7c6cc20f8d9b68bd0f3a8a to your computer and use it in GitHub Desktop.
Read Only property in 'vanilla' JavaScript.
This file contains hidden or 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
class BankAccount { | |
constructor(accountNumber) { | |
Object.defineProperty(this, 'accountNumber', { value: accountNumber }) | |
} | |
} | |
const account = new BankAccount('987-654-321') | |
// 987-654-321 | |
console.log(account.accountNumber) | |
// TypeError: Cannot assign to read only property | |
account.accountNumber = 'invalid' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment