I won't argue that with is a misunderstood feature of JS. I liked it myself. However I'm not a fan of your example. I like with() {} because of the ability to import namespaces into a scope. with(mylibrarysnamespace) { mylibraryfunction(); // called without needing to do mylibrarynamespace.mylibraryfunction(); }
For your example, the counterpart: window.onload = function(){ var body = document.body; body.appendChild(document.createElement("div")) .appendChild(document.createTextNode("hello")); body.firstChild.innerHTML += " world"; }; Is much more efficient. Even if your example did shave off the size (which in this case, it actually makes it longer instead) it's inefficient for the interpreter to run it. You add up to 3 levels onto the scope chain. To get ahold of createTextNode the interpreter has to check for thenodeyoucreated.createTextNode, body.createTextNode, then finally document.createTextNode. As opposed to just document.createTextNode which gets the function you want directly. Also, to be frank I found it hard to read. It took me a bit to decipher it and convert it to that chunk above. By the way, the example falls apart when dealing with fetching nodes and setting attributes that may not be defined. ~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name] Andrea Giammarchi wrote: > "with" is another misunderstood feature of JS, imho, you have to be > truly a junior to make mistakes with that (and we all did common > mistakes when we were junior, is there anybody that blamed the > language rather than hisself?) > The only problem I can spot with "with" is not about developers but > the YUI compressor and machine parsers ... there is nothing wrong > there. I wonder which part is ambiguous for humans, if they know what > they are doing ... > > onload = function(){ > with(document){ > with(body){ > with(appendChild(createElement("div"))) > appendChild(createTextNode("hello")); > with(firstChild) > innerHTML += " world"; > } > } > }; > > ... and script size is preserved ... > > BTW, I have already done all these conversations years ago when I > become certified AS2 developers being a pro with AS1 and Macromedia > decided to change ActionScript (ECMA262) 2 times in 3 years ... good > stuff, except Macromedia does not exist anymore indeed and lost a lot > of money putting effort for something never truly adopted by > developers until somebody else put even more money (Adobe) > > I do not like breaking stuff, ES5 supposes to avoid problems they had > with ES4 but if this is the direction, and this will be 'cause we > cannot do anything except adapt ourself, I do not think it will bring > any concrete benefit for developers except, as I've already said, a > lot of confusion added in the already most confused programming > language ever. > > Regards > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery Development" group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---