Is it possible to access object properties that can only be accessed with the square bracket notation when inside a “with” statement.
Example:
var o = { "bad-property": 1, "another:bad:property": 2, "goodProperty": 3 };
with(o) {
console.log(goodProperty); // works awesome
console.log(???) // how to access "bad:property"?
}
Wow this is old, but the answers here are wrong, there is in fact way to do exactly as you ask.
Did you see it? valueOf() is the magic word. It returns the primitive value of its parent object, or if the object has no primitive value, the object itself. Every object and object-like primitive inherits this method, as it is a built in property on
Object.prototype. So…there you go.