Skip to content

Instantly share code, notes, and snippets.

@HenriqueSilverio
Created August 14, 2025 00:24
Show Gist options
  • Save HenriqueSilverio/ecbddf532a7c6cc20f8d9b68bd0f3a8a to your computer and use it in GitHub Desktop.
Save HenriqueSilverio/ecbddf532a7c6cc20f8d9b68bd0f3a8a to your computer and use it in GitHub Desktop.
Read Only property in 'vanilla' JavaScript.
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