closures

AS3: Loading SWFs in a for loop

I'm trying to load external SWFs in a for loop, and I have this problem that is really eating me: In the event handler I need to know the filename of the SWF that was loaded, but I can't obtain this. The code below shows what I'm trying to do. Does anybody have any idea? function loadManySWFs(arrayOfFileNames:Array) { for(var i=0;...

JavaScript: Closures help

I have an empty .js file with this code in it: Cart.CheckoutNow = { ... } // Alias if (typeof(CheckoutNow) === 'undefined') { CheckoutNow = Cart.CheckoutNow; } else { alert('CheckoutNow is already a variable in window.'); } How can I have the // Alias code appear at the top of the page, but have it execute after Cart.Checko...

Closures in a for loop

Closures in a loop are causing me problems. I think I have to make another function that returns a function to solve the problem, but I can't get it to work with my jQuery code. Here is the basic problem in a simplified form: function foo(val) { alert(val); } for (var i = 0; i < 3; i++) { $('#button'+i).click(function(){ foo(i...

How do Python's class closures work?

If I make a class against a local namespace, how exactly does it work? For instance: >>> def foo(): ... i = 1 ... class bar(object): ... j = i ... return bar ... >>> dis(foo) 2 0 LOAD_CONST 1 (1) 3 STORE_DEREF 0 (i) 3 6 LOAD_CONST 2...

Is it possible to simulate closures in PHP 5.2.x not using globals?

Is it possible to simulate closures in PHP 5.2.x not using globals? I could think of a way that would pass the desired variables as extra parameters to the closure but that just doesn't feel like best practice. Any ideas? ...

Java closure types, variables, arrays and collections

What is the current state of spec for Java closure? 1. In the proposed Java closure spec, would we be able to create an array or collection of closures? If so, would this syntax be possible? {int x, int y => boolean b}[] comparisonSwitch = { {int i, int j => return i>j}, {int i, int j => return j<i}, {int i, int j => return j==...

Closures in C# event handler delegates?

I am coming from a functional-programming background at the moment, so forgive me if I do not understand closures in C#. I have the following code to dynamically generate Buttons that get anonymous event handlers: for (int i = 0; i < 7; i++) { Button newButton = new Button(); newButton.Text = "Click me!"; newButton.Click ...

What is benefit of using (function(){...})() in JavaScript

I noticed in JQuery that the following code structure is used (function(){var l=this,g,y=l.jQuery,p=l.$,...})() Which seems to create a function, and call it. What is the benefit of taking this approach versus having the contents of the function inline? ...

How to declare a javascript class with short function names and assign later to a long class name?

Let me explain, this is about displaying class source code in IDEs. I have always only one class per file. A class is declared like this in the file mod1.js: MYGLOB.MOD1.ClassXy = function () { //constructor, do somothing } MYGLOB.MOD1.ClassXy.prototype.memberfunc1 = function () { //member function 1, do somothing } (There are ma...

Javascript private methods -- what is the memory impact?

I'm working on a bit of code where I'm attempting to hide some private variables inside closures. The thing is the environment is fairly constrained in terms of memory, so I'm also concerned with keeping the overall footprint of the classes low. What is the impact of using closures to hide private instance variables and methods when com...

Does this code really cause an "access to modified closure" problem?

Taking the following code, Resharper tells me that voicesSoFar and voicesNeededMaximum cause "access to a modified closure". I read about these but what puzzles me here is that Resharper suggests fixing this by extracting the variables to right before the LINQ query. But that is where they are already! Resharper stops complaining if I m...

Referring to non-final fields of an enclosing class inside an anonymous inner class in Java

In Java, I know that it is possible to do something like this: public class Greeter { public void greetEventually() { final String greeting = "Hello!"; Job j = new Job() { public void run() { System.out.println(greeting); } }; j.schedule(); } } This would ...

Improve property monitoring code?

Hey folks, I made a utility debug class in a C# game I'm working on to be able to monitor and watch values of properties. Goes like this: public static class Monitor { private static List<object> monitoredObjects; public static void Initialize() { monitoredObjects = new List<object>(); } public static void Watch(object ...

jquery passing a variable to a function

Hay guys, I'm making a simple preload function (function($) { $.fn.preload = function(settings) { var config = { before: function(){ return; }, end: function(){ return; }, after: function(a){ return; } }; if (settings) $.extend(config, settings); var limit = this.length - 1; var counter...

Closure doesn't work

If block is a closure, why this code does not work? And how to make it work? def R(arg) Class.new do def foo puts arg end end end class A < R("Hello!") end A.new.foo #throws undefined local variable or method `arg' for #<A:0x2840538> ...

Fun with Lambdas

Not having them used them all that much I'm not quite sure all that lambdas/blocks can be used for (other than map/collect/do/lightweight local function syntax). If some people could post some interesting but somewhat understandable examples (with explanation). preferred languages for examples: python, smalltalk, haskell ...

What do (lambda) function closures capture in Python?

Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code: adders= [0,1,2,3] for i in [0,1,2,3]: adders[i]=lambda a: i+a print adders[1](3) It builds a simple array of functions that take a single input and return that input added by a number. The funct...

Javascript, odd behaviour in a closure.

Hello, I was toying (read: learning) around with Javascript and came across something to my understanding, seems very odd. It has to do with closures and a reference that seems to 'loose' its importance to the browser. The browser I am using is Chromium 5.0.307.7. Anyway, here's some code: HTMLElement.prototype.writeInSteps = function...

Closure conversion and separate compilation of higher-order function calls

Is there a standard way of dealing with the interaction between separate compilation and different kinds of closure conversion when compiling higher-order function calls? I know of three function-like constructs that are distinctly compiled in most programming languages: closures, (top-level) functions, and C++-style function objects. S...

JavaScript closures and variable scope

I am having trouble with JS closures: // arg: an array of strings. each string is a mentioned user. // fills in the list of mentioned users. Click on a mentioned user's name causes the page to load that user's info. function fillInMentioned(mentions) { var mentionList = document.getElementById("mention-list"); mentionList.innerH...