3 ms·
C# has closures but not when evented. That's the problem with these Language X to js projects. No language is really like JavaScript. Except CoffeeScript, of co
by MatthewPhillips 15y ago
C# has closures but not when evented. That's the problem with these Language X to js projects. No language is really like JavaScript. Except CoffeeScript, of course. I'm sure you can write clean C# dom code but it won't be like js or like Silverlight. It will need new patterns.
- kahoon 15y agoC# has closures but not when evented. What does this mean? How do closures behave any differently when a delegate is assigned as an event handler?
- nxn 15y agovar outer = 2; var btn = new Button() { Text = "Click Me" }; btn.Click += (obj, eventArgs) => { var local = outer + outer; }; How is the above any different/better/worse than what javascript can do?
- MatthewPhillips 15y agoI reserve the right to be wrong here, but I'm pretty sure in your example outer will be be null on the onclick.
- MatthewPhillips 15y agoOk I tested it and you have access to outer here. I must be thinking of something else. Asynchronous web requests?
- chrisb 15y agoouter will be 2 A closure is created for the anonymous function, allowing access (by reference) to the non-local variables in the containing method.
- SideburnsOfDoom 15y agoOuter is (implicitly) typed as an int, so it cannot be null under any circumstances. The default value for ints is 0. However, outer will be 2 in this case. Environment capture makes it do what it looks like, just like javascript. I don't know what you mean by "has closures but not when evented."
- chc 15y agoIf outer were anything but 2 in the click handler, it wouldn't be a closure, which by definition closes over its environment.
- skrebbel 15y agoWhat? C# has closures but the CLR doesn't. For the CLR, it's just yet another object of some class, where the class has a relatively funky name. True about the new patterns, btw. I guess you'd need to port a jQuery to .NET first in order to be productive.
- ken 15y agoAs someone writing a compiler which targets Javascript, I'm curious: what exactly is the problem you are referring to? No language is really like x86 machine code, either, but that doesn't seem to have stopped people from writing anything-to-x86 compilers.