Pretty much every resource documenting with
that I can find warns against using it, mostly because if a variable is not defined it may have unpredictable effects.
I want to understand it so that I can make effective use of it - after all, it's there for a reason. Even eval
has its non-evil uses!
So, with that in mind, let's say I want to delete all child nodes from an element, without using elem.innerHTML = "";
Would the following be safe?
with(elem) while(firstChild) removeChild(firstChild);
Note that at this point I'm not caring for readability, just functionality. Since firstChild
is a property of, and removeChild
a method of, all element nodes, it should be fine to use with
this way, right?
Similarly, let's say I want to set some styles.
with(elem.style) {
color = "red";
backgroundColor = "black";
fontWeight = "bold";
}
Since all of these are properties of the style object (even if not defined in the stylesheet, they are there as empty strings), it's fine to use with
like this, right?
Am I missing something, or is the constant warning to not use with
similar to the one against PHP's mysql
extension: a protection against dumb programmers?