Reputation: 2374
How were implemened constants in javascript
before ES5
was released?
There were no get/set
stuff, no writable
property, no const
word, no Object.freeze
stuff as far as I know, so how I could make my own constant, which can't be changed?
Like, as example, Math.PI
Upvotes: 2
Views: 987
Reputation: 664444
How were constant such as
Math.PI
implemented in javascript before ES5 was released?
Even ES3 knew property attributes - see section 8.6.1 in the spec. There were ReadOnly
, DontEnum
, DontDelete
and Internal
.
In fact, for the Math.PI
property the spec says "This property has the attributes { DontEnum, DontDelete, ReadOnly }."
However, one could (and still can) overwrite the whole Math
object with another one.
How I could make my own constant, which can't be changed?
You could not. There was no way to make variables or objects immutable, for that you needed to wait until ES6 (const
) and ES5 (Object.freeze
) respectively.
Developer discipline was all that prevented constants from being overwritting. Often, style guides include standards for them, like all-uppercase names.
Upvotes: 5