I am a JS newbie, reading a book Javascript Patterns for understanding. One of the code snippets I could see :
var myFunc = function param() {
...
...
};
myFunc.cache={};
This indicates a new property can be added by anyone outside the function body. Doesn’t this break encapsulation ? What if some other part of program keeps on adding new properties making my object creation bulky ? What if someone deletes/modifies properties defined by me ?
I don’t know if I am right in calling this a "problem". But if it is, please help with an approach to avoid this.
Thanks !
JavaScript never really had any options to secure your objects but starting in ECMAScript 5 you can now freeze an object.
There is also a new context known as strict mode where an error can be thrown.