Alan Storm's comments in response to my answer regarding the with
statement got me thinking. I've seldom found a reason to use this particular language feature, and had never given much thought to how it might cause trouble. Now, I'm curious as to how I might make effective use of with
, while avoiding its pitfalls.
Where have you found the with
statement useful?
I created a "merge" function which eliminates some of this ambiguity with the
with
statement:I can use it similarly to
with
, but I can know it won't affect any scope which I don't intend for it to affect.Usage:
My
boils down to
Can you trust so low-quality code? No, we see that it was made absolutely unreadable. This example undeniably proves that there is no need for with-statement, if I am taking readability right ;)
As Andy E pointed out in the comments of Shog9's answer, this potentially-unexpected behavior occurs when using
with
with an object literal:Not that unexpected behavior wasn't already a hallmark of
with
.If you really still want to use this technique, at least use an object with a null prototype.
But this will only work in ES5+. Also don't use
with
.For some short code pieces, I would like to use the trigonometric functions like
sin
,cos
etc. in degree mode instead of in radiant mode. For this purpose, I use anAngularDegree
object:Then I can use the trigonometric functions in degree mode without further language noise in a
with
block:This means: I use an object as a collection of functions, which I enable in a limited code region for direct access. I find this useful.
Visual Basic.NET has a similar
With
statement. One of the more common ways I use it is to quickly set a number of properties. Instead of:, I can write:
This isn't just a matter of laziness. It also makes for much more readable code. And unlike JavaScript, it does not suffer from ambiguity, as you have to prefix everything affected by the statement with a
.
(dot). So, the following two are clearly distinct:vs.
The former is
someObject.Foo
; the latter isFoo
in the scope outsidesomeObject
.I find that JavaScript's lack of distinction makes it far less useful than Visual Basic's variant, as the risk of ambiguity is too high. Other than that,
with
is still a powerful idea that can make for better readability.I am working on a project that will allow users to upload code in order to modify the behavior of parts of the application. In this scenario, I have been using a
with
clause to keep their code from modifying anything outside of the scope that I want them to mess around with. The (simplified) portion of code I use to do this is:This code ensures (somewhat) that the user-defined code neither has access to any globally-scoped objects such as
window
nor to any of my local variables through a closure.Just as a word to the wise, I still have to perform static code checks on the user-submitted code to ensure they aren't using other sneaky manners to access global scope. For instance, the following user-defined code grabs direct access to
window
: