Object.preventExtensions and Object.seal prevent unknown properties from being added to an object, but those attributions silently fail instead of throwing an error. Is there a way to force them to be errors?
var myObj = Object.seal({});
try{
myObj.someProp = 17;
console.log("I don't want to reach this message");
}catch(err){
console.log("I want an error to occur instead.")
console.log("Or at least get a warning somewhere.");
}
I tested this in Chrome 19 and Firefox 9. I wouldn’t mind browser-specific solutions either, since I would only need this during development.
If strict mode is an option, Object.seal seems to do the trick (at least in Firefox):
Works pretty good: http://jsfiddle.net/yVWr6/
(btw: works the same for preventExtensions())