Often I have this situation:
var obj = { field: 2 };
and in the code:
do_something( obj.fiel ); // note: field spelt wrong
ie The property is incorrectly typed. I want to detect these bugs as early as possible.
I wanted to seal the object, ie
obj = Object.seal(obj);
But that only seems to prevent errors like obj.fiel = 2;
and does not throw errors when the field is simply read.
Is there any way to lock the object down so any read access to missing properties is detected and thrown?
thanks, Paul
EDIT: Further info about the situation
- Plain javascript, no compilers.
- Libraries used by inexperienced programmers for math calc purposes. I want to limit their errors. I can spot a misspelt variable but they can't.
- Want to detect as many errors as possible, as early as possible. ie at compile time (best), at runtime with thrown error as soon as wrong spelling encountered (ok), or when analysing results and finding incorrect calculation outputs (very very bad).
- Unit tests are not really an option as the purpose of the math is to discover new knowledge, thus the answers to the math is often not known in advance. And again, inexperienced programmers so hard to teach them unit testing.