let a = document.createElement('a')
with(a) {
setAttribute ('href', 'http://google.com/')
}
console.log(a.getAttribute('href')) // http://google.com/
let object = {
id: 14904,
value: 'Main Street Avenue'
}
with(object) {
console.log(id, value) // 14904, Main Street Avenue
}
let fs = require ('fs')
with(fs) {
readFile('log.txt')
}
I rarely if ever come across someone using with. Some sources consider it deprecated and advise against it but in my experience every JavaScript interpreter understands it and behaves like expected.
Use of the with statement is not recommended, as it may be the source of confusing bugs and compatibility issues. See the "Ambiguity Contra" paragraph in the "Description" section below for details.
The with statement extends the scope chain for a statement.
Source: MDN
What about your experience, are you with me?
Top comments (4)
No. And I'm genuinely curious to find out the original reason for it being added.
with
is one of the biggest blunders about JS as a language and should be removed.It seemed like such a good idea at the time.
Many of the world's most destructive developments started like a good idea.
Honestly, I read about this feature once every few years and just forget what it is and how it works in between. So the answer is a clear no.