Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Crockford warns against ever using `with` in a talk he gave last year. Discussion on `with` at 19m58s: http://ontwik.com/javascript/douglas-crockford-javascript-pr...

Edit: He says it's not that `with` isn't useful, but that there is never a case where it isn't confusing.



That is very well put. It is a shame that JS got its scoping so very upside-down, because it means that tiny changes in code can cause incredibly confusing and initially undetectable bugs. `with` is basically an infected bandaid on a broken system. It seems like it's helping until you notice the smell.


Sure there are - why are so many Crockford pronouncements taken as a signal to stop thinking?

Notice that his "with is bad" examples (which are usually bad examples and bad usages [1]) always involve contrived examples of potentially ambiguous assignment - one of the more useful ways to use `with` is with objects whose properties are DSL-like functions, named in such a way that there's no mistaking their origin and with no ambiguous assignment involved.

Simple example with includes: https://github.com/insin/DOMBuilder/blob/master/examples/red...

More extensive example of inheritable templates for a CRUD admin app: https://github.com/insin/sacrum/blob/master/lib/sacrum/admin...

[1] http://webreflection.blogspot.com/2009/12/with-worlds-most-m...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: