While browsing through this post on read-only properties, I stumbled upon the following code snippet:
var myObject = {
get readOnlyProperty() { return 42; }
};
alert(myObject.readOnlyProperty); // 42
myObject.readOnlyProperty = 5; // Assignment allowed but has no effect
alert(myObject.readOnlyProperty); // 42
I understand that using an IIFE can help in hiding scope and making variables or properties "private". However, what confuses me is:
Why does the assignment work if it doesn't actually change anything? With no explicit scope indicated in this example, I find it puzzling how JavaScript treats a property as private.