tag:blogger.com,1999:blog-34454975.post3584322296099404720..comments2023-06-28T16:58:41.189+02:00Comments on Web Reflection: with: Some Good ExampleAndrea Giammarchihttp://www.blogger.com/profile/16277820774810688474noreply@blogger.comBlogger18125tag:blogger.com,1999:blog-34454975.post-9247927686944822202009-12-11T04:55:38.140+01:002009-12-11T04:55:38.140+01:00Thx @WebReflection.
@Erik - I don't think let...Thx @WebReflection.<br /><br />@Erik - I don't think let and const are apart of ECMAScript 5 but rather being considered for the version after -> see http://developer.yahoo.com/yui/theater/video.php?v=crockford-yuiconf2009-statewavdedhttps://www.blogger.com/profile/16691597009319232644noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-60135139097892865502009-12-07T10:51:52.079+01:002009-12-07T10:51:52.079+01:00Erik I am sure ES5 adopted lot of stuff from Mozil...Erik I am sure ES5 adopted lot of stuff from Mozilla but let, as many other things there, are available since JS 1.7<br /><a href="http://webreflection.blogspot.com/2006/11/not-yield-and-shortest-fibonacci.html" rel="nofollow">example</a><br />Moreover, it's about 4 years I am <a href="http://devpro.it/JSL/" rel="nofollow">trying to normalize</a> Mozilla stuff for other browsers.<br />Eventually ES5 recognized these methods are a must have but I am partially using ES5 since I have created first JSL.<br />RegardsAndrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-25575305411786891042009-12-07T09:22:32.383+01:002009-12-07T09:22:32.383+01:00'let' is part of the ECMAScript 5 standard...'let' is part of the ECMAScript 5 standard, which was approved last week.Anonymoushttps://www.blogger.com/profile/01886315732131336875noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-22715743786641703842009-12-06T21:59:12.826+01:002009-12-06T21:59:12.826+01:00@Marc, it is dangerous because it brings an invisi...@Marc, it is dangerous because it brings an invisible scope that could cause troubles <b>only if you assign variables</b><br />You'll never find somebody screaming how bad is the "with" statement without showing you some assignment.<br />If we talk about performances, this is another story, and I have showed here some example for quick inline extended scope that could demonstrate that "with" is maybe not the best performances option, but it is surely elegant and able to create more compact code - more than any other strategy you could use (YUICompressor, Google Closure Compiler, MS Ajax Minifier, etc etc)Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-17905837654670413132009-12-06T21:52:00.727+01:002009-12-06T21:52:00.727+01:00er ... mate, let is available since 2007 - I am no...er ... mate, let is available since 2007 - I am not saying with is faster, I am saying everybody keep presenting bad usages and examples, so that's fine since that is my whole point, cheers.Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-42828325278014011592009-12-06T21:45:25.118+01:002009-12-06T21:45:25.118+01:00Pardon my ignorance... I never have used "wit...Pardon my ignorance... I never have used "with" since I learned that it was bad to use. These examples were helpful to see how it is useful. Could you give an example of why it is "dangerous" for a novice programmer?wavdedhttps://www.blogger.com/profile/16691597009319232644noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-2267306406811380222009-12-06T21:42:03.681+01:002009-12-06T21:42:03.681+01:00It's faster because you are caching the lookup...It's faster because you are caching the lookup of floor on Math. The function inside the 'with' isolates the var-declared variables, x and i from being looked up in the 'with' scope, so it's not slowed by the 'with'.<br /><br />The tricky thing about 'with' is that is slows down variables that have nothing to do with the 'with' statement. If you look at my examples it's not obvious that the 'with(Math)' has slowed down the accesses to the i and x variables. But it has, by a factor of around 10.<br /><br />You may think my example is nothing like real code. I hope you are right. I boiled it down from a benchmark I found on the net.<br /><br />As for the 'tiny masterpiece' I think it would be clearer with<br /><br />{<br />let o = myreference;<br />o.doStuff();<br />o.var1 = "whataver";<br />// etc etc ...<br />};<br /><br />which you will be able to do in ES5. For now you have to use 'var'.Anonymoushttps://www.blogger.com/profile/01886315732131336875noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-5281627382205883622009-12-06T00:20:15.123+01:002009-12-06T00:20:15.123+01:00and actually, that is faster in IE as well ... sti...and actually, that is faster in IE as well ... still pointless though ...Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-2241338869417789372009-12-06T00:18:59.098+01:002009-12-06T00:18:59.098+01:00Actually ... this is faster in Google Chrome:
with...Actually ... this is <b>faster</b> in Google Chrome:<br /><i>with(Math){<br /> var benchmark = function(){<br /> for (var $floor = floor, x = 0, i = 0; i <= 10000; ++i)<br /> x += $floor(i / 4)<br /> ;<br /> }<br />};</i>Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-43167145204030467692009-12-06T00:04:13.280+01:002009-12-06T00:04:13.280+01:00If performances matters, with is not the best choi...If performances matters, with is not the best choice but, again, that test shows nothing.<br />It is not a real case scenario and "with" used in that way does not make sense.<br /><i>with (Math){<br /> function benchmark(){<br /> for (var $floor = floor, x = 0, i = 0; i <= 10000; ++i)<br /> x += $floor(i / 4)<br /> ;<br /> }<br />};</i><br />Above test is more reasonable, but when performances matter we can use performances strategies:<br /><i><br />function benchmark2(){<br /> for (var $floor = Math.floor, x = 0, i = 0; i <= 10000; ++i)<br /> x += $floor(i / 4)<br /> ;<br />} <br /></i><br />In few words, it's unfair to compare with in this way while my examples show a reasonable usage and benefits.<br />RegardsAndrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-54926247905630918632009-12-05T23:29:29.682+01:002009-12-05T23:29:29.682+01:00If you care about performance, don't use with....If you care about performance, don't use with. http://jsninja.com/Talk:With_StatementsAnonymoushttps://www.blogger.com/profile/01886315732131336875noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-21998168147124221312009-12-05T09:11:10.830+01:002009-12-05T09:11:10.830+01:00About the global scope thing: that's to allow ...About the global scope thing: that's to allow more dependable <b>sandboxing</b>, not to protect <i>you</i> from messing with the global scope, but to protect you from <i>other people</i> messing with the global scope. JavaScript isn't like every other language, because it is the language of the web: scripts are shared, mixed and mashed from third parties all the time, security is playing catch up. I agree with can be useful, though.Zachhttps://www.blogger.com/profile/09507952217085608942noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-39973607360220844542009-12-05T08:14:39.890+01:002009-12-05T08:14:39.890+01:00Brilliant series of posts and useful examples. Thi...Brilliant series of posts and useful examples. This will change the way I write Javascript apps.<br /><br />We can thank the glacial pace of language development and browser adoption for the continued availability of with for at least a few more years...topfunkyhttps://www.blogger.com/profile/03543823423413376077noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-76709177008891847732009-12-05T00:58:46.412+01:002009-12-05T00:58:46.412+01:00I always love your posts Andrea, you aren't af...I always love your posts Andrea, you aren't afraid to take the unpopular side of an issue. These uses of with seems pretty insightful, but I've stayed away from with because everything I've read has said to. maybe I'll play with with now. :)Josh Powellhttps://www.blogger.com/profile/05626242869855645506noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-32730830244557051062009-12-05T00:42:16.941+01:002009-12-05T00:42:16.941+01:00I've spanished your post.I've <a href="http://digitta.com/2009/12/no-toqueis-el-with.html" rel="nofollow"><i>spanished</i></a> your post.alsananhttp://digitta.comnoreply@blogger.comtag:blogger.com,1999:blog-34454975.post-71003476470259440292009-12-04T19:55:48.797+01:002009-12-04T19:55:48.797+01:00They treat us like child, while saying that it'...They treat us like child, while saying that it's for our own good. In the end, they're falling in the dumb user trap: <br />Make an idiot-proof tool, and only idiots will use it. Whatever.<br /><br />I like the examples you wrote, but I have only a minor question:<br /><br />In the with statement, you define "collection" as "collection || []" and then "length" as "collection ? collection.length : 0". This is not ambiguous, but confusing. What collection does "length" refer? We know the answer, but it's somehow messy, isn't it?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-34454975.post-51389559286034191152009-12-04T08:54:17.088+01:002009-12-04T08:54:17.088+01:00the side effect of the statement is the constant s...the side effect of the statement is the constant scope resolution, less performances, missed recursion, unless we don't implement a <i>goto</i> label on the top, and portability, since it strongly depends on the current inner and outer scope.<br />For an inline snippet I may use the statement, e.g. DOM operations, but for performances and frameworks purposes I cannot use it that much.<br />I bet that if with statement was somehow faster than other procedures, more people would have used it.<br />My whole point is that this is a feature few people know or use in a safe/powerful way, and I do not agree at all with the argument:<br />"<i>Since juniors mess up with the statement, we remove the statement</i>".<br />I mean, if this is ES5 way, I won't be surprised if ES5 will make JavaScript the real toy everybody thinks it is.<br />They have removed callee, and caller, because while every other programming language has implemented Reflection, ES5 removed JavaScript introspection, hilarious!<br />I don't like these decision, made by people that do not use, neither know that well, JavaScript, so they just analyze what's more convenient for their parsers or minifiers, avoiding analysis about what developers truly need.<br />It does not matter if we have some prototype cloned by some framework, JavaScript is not used only by "<i>framework people</i>", there are thousands of developers able to create their own framework thanks to JS features.<br />Remove introspection, remove things cause some ignorant wannabe could mess up with things ... it's like they are saying:<i>guys, this language is too good for you while it suppose to be a "toy". You don't study it, you don't understand it, abut you pretend it works as you think.<br />You are all idiots and newbies ... so we remove cool parts because you don't need introspection, neither complex statements you don't understand ... but be happy, we put some prototype framework methods there ...</i><br /><br />Same is for the "this" reference to undefined ... the reason? People could mess up the global scope ... again: <b>we are programmers as you are, not everybody here is a moron</b><br />This is how ES5 is coming out, and I personally do not like it.<br />CheersAndrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-19496208181605040422009-12-04T05:48:45.376+01:002009-12-04T05:48:45.376+01:00These are really entertaining snippets of code (an...These are really entertaining snippets of code (and a very good read), but honestly, do you use any of those techniques on a daily basis?Kamil Trebuniahttps://www.blogger.com/profile/07588232200621621110noreply@blogger.com