Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use "with" keyword in JavaScript? [duplicate]

Tags:

javascript

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?

like image 647
John McCollum Avatar asked Dec 18 '09 23:12

John McCollum


People also ask

What is the use of with keyword in JavaScript?

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.

Can you define a function twice in JavaScript?

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.

Does JavaScript have a using statement?

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.

How many reserved keywords are there in JavaScript?

There are a total of 63 reserved words in JavaScript.


2 Answers

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 writing

ooo.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 and bang will get modifed. Will ooo.eee.oo.ah_ah.ting.tang.walla.walla be modified? Or will the global variables bing and bang 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...

like image 163
azazul Avatar answered Sep 23 '22 15:09

azazul


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".

like image 39
Rich Avatar answered Sep 25 '22 15:09

Rich