Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the JavaScript keyword `with` really deprecated?

Tags:

javascript

Using JavaScript with statements have for a long time been considered bad coding practice, and been advised against.

This page indicates that with is deprecated and will throw an error in strict mode, my browser throws an error when using with in strict mode, and Google Closure Compiler throws an error indicating that strict mode does not allow with:

JSC_USE_OF_WITH: The with statement cannot be used in strict mode.

According to MDN, it is recommended to avoid using with, but as of today, it does not appear to be actually deprecated, and it does not appear to be included in examples of code restrictions of strict mode.

This appears somewhat contradicting and confusing. So, is with really deprecated or not?

like image 333
Tomas Langkaas Avatar asked Mar 06 '23 21:03

Tomas Langkaas


1 Answers

This official stance by the TC39 group (the guys that publish the ECMAScript specification) is insightful:

Why don't we deprecate features?

Deprecation doesn't work on the web. Since we can't remove bad features, a developer has little incentive to stop using a feature just because somebody somewhere doesn't like it. Labeling features "deprecated" without ever removing them is pointless.

http://tc39wiki.calculist.org/about/faq/#deprecate

So, it will probably never be officially deprecated by the ECMAScript standard; however, within the community the consensus that with is bad is strong enough that the big players (i.e. the compiler and engine implementers) have opted to treat it as deprecated if you opt into strict mode and/or opt to use certain strict compilers.

like image 123
deceze Avatar answered Mar 29 '23 04:03

deceze