closures

Javascript 'class' and singleton problems

I have a singleton object that use another object (not singleton), to require some info to server: var singleton = (function(){ /*_private properties*/ var myRequestManager = new RequestManager(params, //callbacks function(){ previewRender(response); }, function(){ previewError(); } ); /*_pu...

What is this kind of mutual "recursion" called?

My issue is with a certain style of code that very much resembles recursion, but isn't quite that. Recursion is, to quote Wikipedia, "a method of defining functions in which the function being defined is applied within its own definition". Similarly mutual recursion applies another function which, directly or indirectly, applies the func...

How are Scala closures transformed to Java objects?

I'm currently looking at closure implementations in different languages. When it comes to Scala, however, I'm unable to find any documentation on how a closure is mapped to Java objects. It is well documented that Scala functions are mapped to FunctionN objects. I assume that the reference to the free variable of the closure must be st...

How to simplify ruby block with one argument?

Somewhere i saw a way to simplify ruby blocks with one argument, it basically omitted vertical bars and argument declaration, because it was somehow inlined. I cannot find it anymore or remember any names t osearch for. Can you help? ...

Does Mootools prevents javascript closure 100%?

Hi, While I was talking about javascript closure to my friend, I was told that using Mootools can prevent closures 100%. To my knowledege, a variable causes a closure. How does Mootools itself prevents javascript closure? I think my friend is saying that Mootools' functions are closure-safe functions. Any suggestions? ...

Javascript Closure question.

Why the following code prints "0 5 10 15 20 ... 100"? (function () { for ( var i = 100; i >= 0; i -= 5) { (function() { var pos = i; setTimeout(function() { console.log(" pos = " + pos); }, (pos + 1)*10); })(); } })(); I declare pos = i , which should be in a descending order. This code ori...

What is a practical use for a closure in JavaScript?

I'm trying my hardest to wrap my head around JavaScript closures. I get that by returning an inner function, it will have access to any variable defined in its immediate parent. Where would this be useful to me? Perhaps I haven't quite got my head around it yet. Most of the examples I have seen online don't provide any real world code,...

In Javascript, when is a new scope created? (with a new function and in a "with" statement) Are these the only 2 situations?

In Javascript, when is a new scope created? The 2 situations I know of are: with a new function in a "with" statement as a note, any new block (in if-then-else, loops, or just beginning a block for no other reason) won't create a new scope. Is there a third situation where a new scope is created besides the two situations above? ...

In languages which create a new scope each time in a loop block, a new local copy of the local loop variable is created each time in that new scope?

It seems that in language like C, Java, and Ruby (as opposed to Javascript), a new scope is created for each iteration of a loop block, and the local variable defined for the loop is actually made into a local variable every single time and recorded in this new scope? For example, in Ruby: p RUBY_VERSION $foo = [] (1..5).each do |i| ...

How can this closure test be written in other languages?

I wonder how the following closure test can be written in other languages, such as C/C++ and Java. Can the same result be expected also in Perl, Python, and PHP? Ideally, we don't need to make a new local variable such as x and assign it the value of i inside the loop, but just so that i has a new copy in the new scope each time. (if p...

In Javascript, a function starts a new scope, but we have to be careful that the function must be invoked so that the scope is created, is that so?

In Javascript, I am sometimes too immerged in the idea that a function creates a new scope, that sometimes I even think the following anonymous function will create a new scope when it is being defined and assigned to onclick: <a href="#" id="link1">ha link 1</a> <a href="#" id="link2">ha link 2</a> <a href="#" id="link3">ha link 3</a> ...

If the "with" statement in Javascript creates a new scope, why does the following code not work as expected? (the closure doesn't contain the new "x" in new scope each time)

If the with statement in Javascript creates a new scope, shouldn't clicking on the links show a different x which are in different scopes? It doesn't... <a href="#" id="link1">ha link 1</a> <a href="#" id="link2">ha link 2</a> <a href="#" id="link3">ha link 3</a> <a href="#" id="link4">ha link 4</a> <a href="#" id="link5">ha link 5</a>...

Why are closures broken within exec?

In Python 2.6, >>> exec "print (lambda: a)()" in dict(a=2), {} 2 >>> exec "print (lambda: a)()" in globals(), {'a': 2} Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> File "<string>", line 1, in <lambda> NameError: global name 'a' is not defined >>> exec "print (lambda: a...

How can I access variables outside of current scope in javascript?

I'm writing an application in javascript and cannot figure it out how to access the variables declared in my function, inside this jquery parse. Inside I can access global variables, but I don't really want to create global vars for these values. Basically I want to extract file names from an xml document in the simulationFiles variable...

Do I need to include the 'this' when using a property name in a closure?

I'm using a list of Actions to store an undo history for an object. Let's say I have a property of my object called myChildObject and it's being mutated by a method call, so I want to store the undo action where I would mutate it back to it's current value: public class Class1 { public Class1() { } private readonly Lis...

Does any language (or debugging tool) have a build in function or method to print out the scope chains, so as to look at the different situations of what a scope chain contains?

Does any language or debug tool have a way to print out the scope chain for examination? Thanks. ...

When actually is a closure created?

Is it true that a closure is created in the following cases for foo, but not for bar? Case 1: <script type="text/javascript"> function foo() { } </script> foo is a closure with a scope chain with only the global scope. Case 2: <script type="text/javascript"> var i = 1; function foo() { return i; } </script> same a...

Python Closures Example Code

I am learning Python using "Dive Into Python 3" book. I like it, but I don't understand the example used to introduce Closures in Section 6.5. I mean, I see how it works, and I think it's really cool. But I don't see any real benefit: it seems to me the same result could be achieved by simply reading in the rules file line by line in a...

How does java implement inner class closures?

In Java an anonymous inner class can refer to variables in it's local scope: public class A { public void method() { final int i = 0; doStuff(new Action() { public void doAction() { Console.printf(i); // or whatever } }); } } My question is how is this actually...

Are closures in javascript recompiled

Let's say we have this code (forget about prototypes for a moment): function A(){ var foo = 1; this.method = function(){ return foo; } } var a = new A(); is the inner function recompiled each time the function A is run? Or is it better (and why) to do it like this: function method = function(){ return this.foo; } function A...