tag:blogger.com,1999:blog-1720439865889190419.post1259467710293470812..comments2023-03-25T13:51:50.278+01:00Comments on C Spangled: Variable closure in JavascriptAlberthttp://www.blogger.com/profile/15599692262672037932noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-1720439865889190419.post-20700697219857570072011-09-07T13:15:16.162+02:002011-09-07T13:15:16.162+02:00Yes but everyone else knows that nothing should ha...Yes but everyone else knows that nothing should happen after the return function, so you are making the code even harder to maintain by someone else by using this hole in javascripts logic. (They'd probably go "These null decelerations never happen" and delete it, and then it has global scope again).<br /><br />The true magic of var in local function scope is that it has no scope after the function is executed by declaring a variable with var initially, so you don't even have to write null statements after the return, which I think is amazing.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1720439865889190419.post-27798523181366465682011-09-06T20:56:34.310+02:002011-09-06T20:56:34.310+02:00@Anonymous of 6th September
They DO indeed get ex...@Anonymous of 6th September<br /><br />They DO indeed get executed after the return statement - that's the magic of "var"! Try removing them and observe changed behaviour.Alberthttps://www.blogger.com/profile/15599692262672037932noreply@blogger.comtag:blogger.com,1999:blog-1720439865889190419.post-10176279751728932011-09-06T17:22:30.102+02:002011-09-06T17:22:30.102+02:00You signal your intent to use a variable in the fu...You signal your intent to use a variable in the function scope by declaring it with var, that isn't tidy at all. Because you don't ever descope anything, because everything has scope, it just depends if its the right one.<br /><br />Also your vars after your return statement don't get executed because its after your return statement.<br /><br /><br />Nice troll!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1720439865889190419.post-52447026676725988542011-08-05T21:45:51.361+02:002011-08-05T21:45:51.361+02:00Anonymous #3 here again:
NEVER NEVER NEVER put th...Anonymous #3 here again:<br /><br />NEVER NEVER NEVER put the var stuff down at the end. That is ugly and almost as wrong as you can get (very un-idiomatic).<br /><br />function() { foo = 1; ...snip...; var foo = null; }<br /><br /><br />Javascript actually does something called "variable hoisting". Anybody with even a *passing* understanding of javascript will see that initial "foo = 1" and curse your name for polluting the global namespaces. They *might* see the ending "var foo = null", but to the JS engine, it is actually red as:<br /><br />function() { var foo = null; /* hoisted */; foo = 1; ...snip...; }<br /><br />Doing it the way you are suggesting is totally wrong because you are absolutely *NOT* "de-scoping" or "garbage-collecting" the variable, and instead are just confusing future readers.<br /><br />But... your code, your rules... who am I to stop you? :^)<br /><br />--RobertAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-1720439865889190419.post-71462672125427876302011-08-04T11:34:56.603+02:002011-08-04T11:34:56.603+02:00It's actually not so strange...
In Javascript...It's actually not so strange...<br /><br />In Javascript, unless you use "var" infront of a variable declaration, it's a global variable that you can use outside the function/scope.<br /><br />If you don't use "var" infront of "idx" when you define it, you are using the "idx" variable in the global scope instead.<br /><br />It's how the language is defined, and it's how other languages work as well (e.g. "local variablename = 1" in LUA).Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1720439865889190419.post-66194865773137058372011-08-04T00:00:36.738+02:002011-08-04T00:00:36.738+02:00I'm not sure how this is considered a hack.......I'm not sure how this is considered a hack....when this is how the language is defined...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1720439865889190419.post-1890033357974296642011-08-03T23:07:23.380+02:002011-08-03T23:07:23.380+02:00Wow! I'm surprised and honoured that someone ...Wow! I'm surprised and honoured that someone already spotted this blog entry posted this to Reddit, especially since I haven't tried to spread the word myself yet. :)<br /><br />Thanks for the tip, Anonymous#3! I've tested putting the "var" on top, and it seems to work equally well everywhere. It also sometimes works putting it in the middle! Javascript, go figure...<br /><br />So I guess where you put it is just a matter of personal preference. In my opinion, putting it at the end is a bit tidier since it better signals my intent: ie. de-scoping the variable after use.Alberthttps://www.blogger.com/profile/15599692262672037932noreply@blogger.comtag:blogger.com,1999:blog-1720439865889190419.post-43168348437712102592011-08-03T21:05:37.283+02:002011-08-03T21:05:37.283+02:00Your explication is completely wrong. It is the &...Your explication is completely wrong. It is the 'var' that is fixing it, not the '= null'<br /><br />In javascript, declarations are per function, so adding the var at the end is the same as adding at the top.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1720439865889190419.post-18274222609520101612011-08-03T20:54:48.190+02:002011-08-03T20:54:48.190+02:00http://www.reddit.com/r/programming/comments/j7z2y...http://www.reddit.com/r/programming/comments/j7z2y/the_secret_to_scoping_in_javascript_magic_var/c29w2ylAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-1720439865889190419.post-71798058939813380882011-08-03T20:50:20.877+02:002011-08-03T20:50:20.877+02:00Eh, why not just put "var" in front of t...Eh, why not just put "var" in front of the variable name at the point of declaration instead?<br /><br />Makes for much shorter code.<br /><br />Unless you want to make Javascript look worse that it actually is of course.Anonymousnoreply@blogger.com