closures

What would you call "callback" or "closure" in general?

Which keyword / tag should we use to describe them when we're taking notes for them. If they are placed under the category "software design", is it appropriate? @edit: It's more about how you category things. When you are in learning, some terminology appears, so what category will you assign it in your own ontology? ...

groovy map coercion in python

I'm a relative newcomer to python and I'm just wondering if there is some equivalent to the map coercion feature available in groovy. For context, I am writing a unit test and want to mock a class with a simple two method interface, in groovy I would do the following: mock = [apply:{value -> return value*2 }, isValid:{return true}] tes...

Function values in JavaScript

nextplease.init = function() {...} is a function with no arguments. I'd expect nextplease.init and function() {nextplease.init();} to behave identically. Is there any possible difference between them (obviously, you can assign something to nextplease.init, but let's exclude that)? In particular, can there be a difference in behavior bet...

Javascript: Get access to local variable or variable in closure by its name

Hi all. We all know that you can access a property of a javascript object by it's name using the [] syntax.. e.g. ob['nameOfProperty']. Can you do the same for a local variable? Another answer here suggested the answer is to use window['nameOfVar']. However, this only worked for the poster as he was defining variables at window-level ...

Distinguishing closure and local variables

A local function in the closure declares a variable with the same name which exists in the closure. So, how could we access closure's variable from the local function? function closure() { var xVar; function func1() { var xVar; // how to distinguish local and closure scopes. return xVar; } re...

Push method only works once unless variable is used before

This might be a weird question. Anyway, I'm working on a jQuery plugin and this is a useful piece of code for this question, note that it's not all ;) : Javascript: (function($){ $.fn.newPlugin = function(){ var ranges = [], selectableRange = this; function init(){ selectableRange.live('mousedown', func...

i cant get my javascript eventlistener to work properly

this has got me stumped, ive tried lots of different things, but cant get this to work. can anyone help? no matter what i try i cant get the click eventlistener on the link to fire. the code is in a greasemonkey script. i believe i have to use the closure method to be able to refer to the function dropit in the greasemonkey script, as it...

How to copy a variable in JavaScript?

I have this JavaScript code: for (var idx in data) { var row = $("<tr></tr>"); row.click(function() { alert(idx); }); table.append(row); } So I'm looking through an array, dynamically creating rows (the part where I create the cells is omitted as it's not important). Important is that I create a new function which encloses...

How is Lexical Scoping implemented?

A couple of years ago I started writing an interpreter for a little Domain Specific Language which included programmer-defined functions. At first I implemented variable scope using a simple stack of symbol-tables. But now I want to move to proper lexical scoping (with the option of closures). Can anyone explain or point me at a good e...

javascript curtain (used to idicate busy process) not displaying for entire process. Probaly I dont understand the callback wll enough.

I have a process where a user puts in a comma delimited list that is then processed one item at a time. I want to be able to indicate to the user that it is processing and let them know when it is done. So I used the curtain idea from Borgar's replay to ... Div Over Page. This worked but the curtain disappears well before the process is ...

New closure on scriptblock

Consider this code: PS> $timer = New-Object Timers.Timer PS> $timer.Interval = 1000 PS> $i = 1; PS> Register-ObjectEvent $timer Elapsed -Action { write-host 'i: ' $i }.GetNewClosure() PS> $timer.Enabled = 1 i: 1 i: 1 i: 1 ... # wait a couple of seconds and change $i PS> $i = 2 i: 2 i: 2 i: 2 I assumed that when I create new clo...

JS: using 'var me = this' to reference an object instead of using a global array

The example below, is just an example, I know that I don't need an object to show an alert box when user clicks on div blocks, but it's just a simple example to explain a situation that frequently happens when writing JS code. In the example below I use a globally visible array of objects to keep a reference to each new created HelloObj...

C# - closures over class fields inside an initializer?

Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { var square = new Square(4); Console.WriteLine(square.Calculate()); } } class MathOp { protected MathOp(Func<int> calc) { _calc ...

Creating a function reference that has value parameters not references

I'm not sure exactly how to describe what I want. I want to define a function with the parameters being a local VALUE not a reference. say I have list of objects I want to create for(i = 0; i < 10; i++){ var div = document.createElement("div"); div.onclick = function(){alert(i);}; document.appendChild(div); } Now I believe in ...

Understanding a skeleton of jQuery plugin

At a website, I found the following code to make a jQuery plugin: (function($){ // Our code here... })(jQuery); I don't understand how the code above works. What I understand is that the code executes immediately because the very last () in function(){}(). So the entire code says that is an anonymous function that is run immediately...

Closure and Callbacks

Is there any other way in java to implement call backs apart from inner classes? What is the difference between callbacks and closures? ...

Inheritance of closure objects and overriding of methods

I need to extend a class, which is encapsulated in a closure. This base class is following: var PageController = (function(){ // private static variable var _current_view; return function(request, new_view) { ... // priveleged public function, which has access to the _current_view this.execute = function() { ...

How to emulate closures in c

Is there a simple way? ...

What rules govern the copying of variables in Javascript closures?

I'd just like to check my understanding of variable copying in Javascript. From what I gather, variables are passed/assigned by reference unless you explicitly tell them to create a copy with the new operator. But I'm a little uncertain when it comes to using closures. Say I have the following code: var myArray = [1, 5, 10, 15, 20]; var...

Dealing with the lack of closures in Objective-C

Maybe it's just the fact that I've been using http://nodejs.org/ lately, but the lack of closures in Objective-C (iphone) has been really hard to work around. For example, I'm creating service classes. Each service class can have several methods, each of which makes a different URL request. I can use the delegate pattern, but that mean...