Possible Duplicate:
Are there legitimate uses for JavaScript’s “with” statement?
I recently discovered that in JavaScript, one can do something like the following:
with (document) { write('foo'); body.scrollTop = x; }
The down side of this is that each variable needs to be checked to see if it belongs to the document object, creating a significant overhead.
Alternatively, one could do something like this:
var d = document; d.write('foo'); d.body.scrollTop = x;
Are there any situations where the use of the 'with' keyword is justified?
The with keyword is used as a kind of shorthand for referencing an object's properties or methods. The object specified as an argument to with becomes the default object for the duration of the block that follows. The properties and methods for the object can be used without naming the object.
functions are data in memory stack, so when you define another function with the same name, it overrides the previous one. Show activity on this post. Well obviously you're not meant to define the same function twice. However, when you do, the latter definition is the only 1 that applies.
The WITH statement is used to specify the default object for the given property and allow us to prevent writing long lengthy object references. It adds the given object to the head of the scope chain.
There are a total of 63 reserved words in JavaScript.
Just don't use it: http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/
JavaScript's
with
statement was intended to provide a shorthand for writing recurring accesses to objects. So instead of writingooo.eee.oo.ah_ah.ting.tang.walla.walla.bing = true; ooo.eee.oo.ah_ah.ting.tang.walla.walla.bang = true;
You can write
with (ooo.eee.oo.ah_ah.ting.tang.walla.walla) { bing = true; bang = true; }
That looks a lot nicer. Except for one thing. There is no way that you can tell by looking at the code which
bing
andbang
will get modifed. Willooo.eee.oo.ah_ah.ting.tang.walla.walla
be modified? Or will the global variablesbing
andbang
get clobbered? It is impossible to know for sure...If you can't read a program and be confident that you know what it is going to do, you can't have confidence that it is going to work correctly. For this reason, the
with
statement should be avoided...
Despite advice to the contrary almost everywhere, I think that there are uses for "with". For example, I'm working on a domain model framework for Javascript, which uses the underscore character in much the same way that jQuery uses "$". This means that without "with", I have lots of underscores scattered through my code in ways that make it less readable. Here's a random line from an application using the framework:
_.People().sort(_.score(_.isa(_.Parent)),'Surname','Forename');
whereas with "with" it would look like
with (_) { ... People().sort(score(isa(Parent)),'Surname','Forename'); ... }
What would be really useful is a read-only version of "with".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With