closures

Issues with closure and a method defined elsewhere.

I am pretty new at Javascript so I may not be using the exact terminology. Suppose that I define an object literal as such. var myObj = { someMethod:function() { //can we have access to "someValue" via closure? alert(someValue); } } And then we assign the function to another object like this. var myOtherObject = ...

Java mechanisms at use in lambdaj closures

Lamdbaj allows the definition of closures in the Java language, various examples can be found here My question is regarding the underlying Java mechanisms at use, for instance, to define the println closure, the following code is used: Closure println = closure(); { of(System.out).println(var(String.class)); } This closure can be s...

Is a Python closure a good replacement for `__all__`?

Is it a good idea to use a closure instead of __all__ to limit the names exposed by a Python module? This would prevent programmers from accidentally using the wrong name for a module (import urllib; urllib.os.getlogin()) as well as avoiding "from x import *" namespace pollution as __all__. def _init_module(): global foo import ba...

calling F# functions from C# and getting null reference exceptions

I'm using Visual Studio 2008 with the October 2009 F# CTP installed. I'm trying to call some F# code from my C# program. Most types of F# functions seem to work, but some are not getting initialized in F# and are throwing NullReferenceExceptions. The ones doing this are closures and partially applied functions, i.e. things that appear...

Is it possible to call a function defined inside a closure?

In the following code, I can call baz. Also somewhere else I read "JavaScript has function-level scope". I know, Im confusing myself somewhere. Can somebody make me understand please? /* An anonymous function used as a closure. */ var baz; (function() { var foo = 10; var bar = 2; baz = function() { return foo * bar;...

how to get an anonymous function to keep the scoping it had originally when called in an event handler

I have a setup where I get some information, in an ajax call, then I immediately use some of that information for another call, and then I populate some fields. The problem is that I am not certain how to create the anonymous function so that it can call this.plantName. Right now the value of this.plantName is undefined. I know that t...

Problem with delegates in C#

In the following program, DummyMethod always print 5. But if we use the commented code instead, we get different values (i.e. 1, 2, 3, 4). Can anybody please explain why this is happenning? delegate int Methodx(object obj); static int DummyMethod(int i) { Console.WriteLine("In DummyMethod method i = ...

Groovy FindAll statement for finding values that don't exist.

Hi. I am trying to construct a Groovy statement to find values that don't exist in a pre-populated list. I'm using SQL and think I want to do something like : myList = [a, b, c, d, e ... lots more data] sql.findAll("SELECT * FROM table WHERE code not in " + <myList>) I have a feeling this is very simple .. I'm just not sure how...

Closure problem? - passing current value of a variable

I'm trying to pass the current value of a variable when an a dynamically generated navigation 'node' is clicked. This needs to just be an integer, but it always results in the last node's value.. have tried some different methods to pass the value, a custom event listener, a setter, but I suspect it's a closure problem.. help would be ap...

JavaScript variable binding and loop.

Consider such loop: for(var it = 0; it < 2; it++) { setTimeout(function() { alert(it); }, 1); } The output is: => 2 => 2 I would like it to be: 0, 1. I see two ways to fix it: Solution # 1. This one based on the fact that we can pass data to setTimeout. for(var it = 0; it < 2; it++) { setTimeout(function(data...

How do you explain closure to a 5 year old?

I am currently looking at lambda expression and the word closure keeps coming. Can someone explain it to me in real simple language. ...

Closure in Javascript

I have 100 elements with ids divNum0,...,divNum99. Each when clicked should call doTask with the right parameter. The code below unfortunately does not close i, and hence doTask is called with 100 for all the elements. function doTask(x) {alert(x);} for (var i=0; i<100; ++i) { document.getElementById('divNum'+i).addEventListener('c...

Scope with a self-invoking function in Javascript

Take below code iterates over 6 input buttons and attaches an onclick event to every button that alerts the index number of the respective iteration: for (var i = 1; i < 6; ++i) { var but = document.getElementById('b_' + i); (function (el) { var num = i; but.onclick = function () { alert(num); ...

How|Where are closed-over variables stored?

This is a question based on the article "Closing over the loop variable considered harmful" by Eric Lippert. It is a good read, Eric explains why after this piece of code all funcs will return the last value in v: var funcs = new List<Func<int>>(); foreach (var v in values) { funcs.Add(() => v); } And the correct version loo...

Is nested XMLHttpRequests with multiple closures a good idea?

I have a Greasemonkey script which operates on a search results page at a video site. The function of the script is to take a javascript link that opens a new window with a flash player, jump through some redirection hoops, and insert a regular link to the desired FLV file. I have changed the script to do silly but structurally equival...

Javascript closure / variable scope question - I know it works, but why?

Hi, I've been developing with JS for a while, and while I know that code below works, I don't really understand why it works. The way I see it, I've defined testString in testClosure function, and I'm expecting that variable to 'go away' when testClosure function is done, since it's local variable. However, when I call inner functio...

How to pass external variables to a private javascript outer closure function?

I may have made some poor design choices on this one. I have several objects being instanced like this. core.modules.trial = function(sandbox){ return{ alert_private : function(){ alert(omgpi); } }; }; I would like to do this: core.modules.trial[omgpi] = "external private var"; var trial = core.modules.trial...

(javascript) why do i need to use a wrap function for event handlers?

I'm trying to understand why in the following code I need Dragger.prototype.wrap and why I can't just use the event handling methods directly: function Dragger(id) { this.isMouseDown = false; this.element = document.getElementById(id); this.element.onmousedown = this.wrap(this, "mouseDown"); } Dragger.prototype.wrap = func...

What is the name for this usage of delegate in C#?

This is a terminology question. In C#, I can do this: delegate Stream StreamOpenerDelegate(String name); void WorkMethod(StreamOpenerDelegate d) { // ... } void Exec1() { WorkMethod((x) => { return File.OpenRead(x); }); } void Exec2() { StreamOpenerDelegate opener = (x) => { ...

closures mean fully type-safe criteria?

combining closures (FCM) and generics, would it be possible to have fully type-safe criteria. // The following works without a cast as Foo.id is a 'long' field. List<Long> ids = session.createCriteria(Foo.class) .setProjection(Foo#id) .list(); // The following is a compilation error, as F...