Is there any way in JavaScript to throw an error when I'm trying to access non existent object property?
inst.prop //Error
Is there any way in JavaScript to throw an error when I'm trying to access non existent object property?
inst.prop //Error
Not presently, no, not on the object itself. ES2015+ offers proxies, and if you wrap the object in a proxy, you can then have the proxy throw an error when you try to read a property through the proxy that the underlying object doesn't have. But that would require wrapping in a proxy, and would require ES2015 support in the environment you're using (proxies can't be polyfilled). All up-to-date major JavaSript engines support proxies. If you're doing this on the web, the engines in slightly older browsers (IE11 for instance) don't support proxies and they cannot be polyfilled.
I'm not advanced or well-read enough to speak on the use of proxies. If I needed this type of functionality, I would opt to create a class with private properties and a getter method. The getter method would throw an error if an invalid key was passed.