|
|
|
|
|
by trealira
936 days ago
|
|
I think it also makes sense in Python, where it can be used to automatically close a file, or with open(filename) as my_file:
data = my_file.read()
Or with acquiring and releasing a lock: lock = threading.Lock()
with lock:
pass # do stuff
That being said, I prefer the C++ approach of using constructors and destructors to automatically acquire resources (like locks and files) by declaring variables within some block scope, and release them once the scope is left. |
|
Javascript with comes from Pascal with (which was perhaps copied from somewhere else) and is used to modify variable scope.
is equivalent to: or at least, it might be ... due to Javascript's dynamic and typeless nature you might not know for sure what will happen until runtime. Which is one reason it's now deprecated and not allowed in strict mode. In pascal, variables have a type, records have fields, and these are known at compile time so that's not an issue.https://wiki.freepascal.org/With
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...