DEV Community

JavaScript's Forgotten Keyword (with)

Randall on July 29, 2021

Being a JavaScript developer can be an exciting job. Almost every day you will find something mysterious and otherworldly. Sometimes that's a magic...
Collapse
 
conradsollitt profile image
Conrad Sollitt •

An example of the one use case I can think of is for simple templating. For example Underscore uses it on the template() function which can be called from modern JS:

underscorejs.org/docs/underscore-e...

Personally I've used it only 1 time I remember after doing JS for 20+ years and it was to create a templating feature for Web Components.

Collapse
 
tadeassoucek profile image
tadeassoucek •

Pascal has a with ... do ... statement, which behaves in the same way as in JS.

Collapse
 
togakangaroo profile image
George Mauer •

I've used it on a project where we created an in browser ide for days analysis. You would configure an object with properties for pluggable builtins, then surround the user's code in a with statement and emit it into a sandbox iframe.

Collapse
 
froxx93 profile image
Froxx93 •

Wow, this sounds absolutely horrible from today's point of view 😅 I wonder what the original idea behind it was. I mean what made the devs think something like this would be necessary or improve the language?

Collapse
 
zevanrosser profile image
Zevan Rosser •

I always liked with :D... how about labels? I remember being weirded out the first time I saw them: developer.mozilla.org/en-US/docs/W...

Collapse
 
kenovienadu profile image
Ovienadu Ken •

I saw this in a c# codebase a few days ago.

Funny how it's usage is similar

Collapse
 
sangdt profile image
meomeo • • Edited

This bring back so much memory when i was young and messing around with vb.net.

Collapse
 
miketalbot profile image
Mike Talbot ⭐ •

If only JS with worked like the vb.net it would be much more useful and no accidental shadowing :)

Collapse
 
zyabxwcd profile image
Akash •

You have a good sense of humour :D. Nicely written article.

Collapse
 
djlazz3 profile image
Bryan Padron •

The only time that I've used this is when in the node console where you can run
with(process) { exit() }, but .exit is definitely a better way to exit

Collapse
 
yw662 profile image
yw662 •

Or let's make it process?.exit(), then you can use it anywhere :-)

Collapse
 
samuelfaure profile image
Samuel-Zacharie FAURE •

"If you use the with keyword, everyone will think you are crazy and avoid you in the lunch room. Or maybe they will just look at you funny."

As they should.