Posts

Showing posts with the label with

with: Some Good Example

OK, I have said my last post would have been the last one about the with statement ... well, before I close the argument, I would like to show a couple of common good examples. Solve References Problem This is what somebody defined a tiny masterpiece with({o:myreference}){ o.doStuff(); o.var1 = "whatever"; // etc etc ... }; Above example annihilate every blame about the not sure if that is the var I meant to use since there is an explicit reference as is, as example, in Python programming language (until version 3.11) // Python with open("x.txt") as f: data = f.read() // JavaScript equivalent with({f:/* as */open("x.txt")}) data = f.read() Memory Safe Operations Another recycled example before I show more interesting stuff ... apparently the whole problem is about write silly code inside with , as if everybody has to define variables or assign stuff inside this statement. Hilariously, I have basically never assigned anything inside a...

with: The World's Most Misunderstood Statement

Update If after this post and its comments you are not convinced yet, have a look into with: Some Good Example one. Every reference to this post is purely casual ... no it's NOT! It's time to talk about the fuss around the innocent with statement, and why somebody decided it's bad or it should not be part of next JavaScript. Extends the scope chain for a statement Extended chains are probably what we love more about JavaScript libraries ... $("stuff") .more() .again() .somethingElse() ; but for some sadistic reason we decided that we don't like native extended chains in our code ... with(stuff){ more() again() somethingElse() }; Bad Examples And Bad Usages Every time I say that with statement has nothing bad, somebody perpetually points out this bloody post ... OK, from Yahoo! ... so what? Honestly, I could post thousands of bad examples and point my finger into this or that library, function, method, evaluation, etc etc ... it's like th...