﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>joe commented on Javascript, lexical scopes and what your momma thought you about variables</title><description>Another way to deal with this is to attach the value you want to the object that it is being used by. Something like this:
  
  
		var alertLink = document.createElement("a");
  
		alertLink.href = "#";
  
		alertLink.appendChild(document.createTextNode(nums[i]));
  
		alertLink.num = nums[i];
  
  
		if( nums[i] % 2 == 0)
  
		{
  
			alertLink.onclick = function() { alert('EVEN: '+ this.num); };
  
		}
  
		else
  
		{
  
			alertLink.onclick = function() { alert('ODD: ' + this.num); };
  
		}
  
  
  
That way the scope of the variable is the same as the scope of the object using it.
</description><link>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment8</link><guid>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment8</guid><pubDate>Wed, 19 Dec 2007 17:29:24 GMT</pubDate></item><item><title>Ted Elliott commented on Javascript, lexical scopes and what your momma thought you about variables</title><description>I've run into this before as well.  Not sure why they decided to implement it this way, it seems to differ from every other language I've ever worked with.  Well except for VB6 I guess, it didn't really have block level scoping, just declare the variable before you need it.
  
  
As far as Andrey's comment about Unit testing, with a port of RhinoMocks as well and a standalone JavaScript engine you could unit test all of Javascript outside of the browser.  There's JSUnit, but alas you have to fire up a browser and run it.  I don't think you can UnitTest Javascript that's embedded in your page either.
</description><link>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment7</link><guid>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment7</guid><pubDate>Fri, 14 Dec 2007 19:30:46 GMT</pubDate></item><item><title>Luke Breuer commented on Javascript, lexical scopes and what your momma thought you about variables</title><description>The "let" keyword in JS 2.0 (or maybe even 1.7, I don't keep them straight) allows one to have curly-based lexical scopes.
</description><link>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment6</link><guid>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment6</guid><pubDate>Thu, 13 Dec 2007 15:28:38 GMT</pubDate></item><item><title>Steve commented on Javascript, lexical scopes and what your momma thought you about variables</title><description>Yeah, that stumped me a few times too.
  
  
Morris Johns wrote a great article with interactive examples at:
  
http://blog.morrisjohns.com/javascript_closures_for_dummies
</description><link>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment5</link><guid>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment5</guid><pubDate>Thu, 13 Dec 2007 13:49:53 GMT</pubDate></item><item><title>Andrey Shchekin commented on Javascript, lexical scopes and what your momma thought you about variables</title><description>Sorry, I probably had a morning of bad English syntax today.
  
I know why people prefer C# -- for static typing. Large JS projects have scaling problems for large teams -- it is just too easy to break something somewhere (at least until someone does a good MbUnit/NMock2 port to JS. that is not so hard btw, but nobody did andd I have a trouble).
  
  
What I was talking about is that JS samples from people knowing .NET are generally of lower quality than their C# samples (do not know about Java or whatever).
  
  
Of course I understand that this is made to demonstrate the point. This is why I marked it as nitpicking). I just like that your C# samples are generally very creative and interesting even in the parts that do not demonstrate the point.
</description><link>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment4</link><guid>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment4</guid><pubDate>Thu, 13 Dec 2007 08:20:59 GMT</pubDate></item><item><title>Ayende Rahien commented on Javascript, lexical scopes and what your momma thought you about variables</title><description>I don't know why people say that C# is better than JS. Perhaps because they are familiar with it?
  
  
You do realize that this is code written to show certain behavior, right? Not a case of showing JS best practices
</description><link>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment3</link><guid>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment3</guid><pubDate>Thu, 13 Dec 2007 07:25:52 GMT</pubDate></item><item><title>Andrey Shchekin commented on Javascript, lexical scopes and what your momma thought you about variables</title><description>Nice post, I have not completely get it in the head myself. 
  
  
Nitpicking:
  
Why for so many people JS code is so much worse than their C# code?
  
  
JS allows much better DSLs and has great frameworks.
  
Personally, I would use something like map() on nums and, also, put the oddness in the link itself, which would allow me to reuse the whole onclick function.
</description><link>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment2</link><guid>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment2</guid><pubDate>Thu, 13 Dec 2007 07:23:55 GMT</pubDate></item><item><title>miles thompson commented on Javascript, lexical scopes and what your momma thought you about variables</title><description>Good post, this stuff  can get confusing at times. To be brutally honest when confronted with a situation like this, I often just hack it till it works (which, without casting aspersions, looks like maybe is what happened here? ;-). 
  
  
In any event the feeling that I don't fully *get* how lexical scope works inside of closures certainly niggles. Need a better  way to look at this problem.
  
  
PS You didn't 'fix' the tmpODD version. Is that just a typo?
  
</description><link>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment1</link><guid>http://ayende.com/3042/javascript-lexical-scopes-and-what-your-momma-thought-you-about-variables#comment1</guid><pubDate>Thu, 13 Dec 2007 01:53:23 GMT</pubDate></item></channel></rss>