closures

Will creating a function within a constructor function use more memory than referencing a prototype function?

Possible Duplicate: JavaScript: Setting methods through prototype object or in constructor, difference? I guess this is a question about the browsers implementation of closures really. I know about the numerous ways to emulate Class-like functionality within JavaScript including using various libraries. This is more a questi...

Odd closure behavior

For some reason, the following bit of code isn't behaving as I would anticipate -- likely due to a misunderstanding on my end as to how it should behave. var contentPane = widget.children("div.content").first(); var success = function (content) { return function (data, successCode, httpRequest) { content.innerHTML = data; ...

Captured variable in C# function with a List<> parameter

I have the following sample codes. I don't like to have lTest being captured and get the same value as lRet. I tried to introduce "List<Test> lTest1 = lTest0;" in the function AddMore2Test. It does not work. What is the proper way to do this? Thanks! Codes - private void Test(){ List<Test> lTest = GetInitTestList(); List<Test...

How are closures implemented in Python?

"Learning Python, 4th Ed." mentions that "the enclosing scope variable is looked up when the nested functions are later called.." However, I thought that when a function exits, all of its local references disappear. def makeActions(): acts = [] for i in range(5): # Tries to remember each i acts.append(lambda x: i ** x) #...

Strange things in JavaScript "for"

Hi. I'm using jQuery and I have a strange thing that I don't understand. I have some code: for (i = 1; i <= some_number; i++) { $("#some_button" + i).click(function() { alert(i); }); } "#some_button" as the name says - they are some buttons. When clicked they should pop-up a box with it's number, correct? But they don'...

MOOTOOLS variable scope

I'm using mootools: I can't figure out how to use a variable when using an addEvent. I want to use a for next loop to set values in a loop: for (x=0;x<num;x++){ var onclickText = 'function (){onclick="addPageMoveEvent('+x+'"); }'; $('pageNum'+x).addEvent('click', onclickText); } > I've search forums but not found any help. Any help...

Odd (loop / thread / string / lamda) behavior in C#.

I have a snippet of code that I though should work because of closures. However the result proves otherwise. What is going on here to not produce the expected output, one of each word. Code: string[] source = new string[] {"this", "that", "other"}; List<Thread> testThreads = new List<Thread>(); foreach (string text in source) { tes...

What is the type of a lambda function?

In C++0x, I'm wondering what the type is of a lambda function. Specifically: #include<iostream> type1 foo(int x){ return [x](int y)->int{return x * y;}; } int main(){ std::cout<<foo(3)(4);//would output 12 type2 bar = foo(5); std::cout<<bar(6);//would output 30 return 0; } What do I need to replace type1/type2 with to get the...

referencing self within a block IPhone SDK

I'm trying to implament a callback mechanism where I pass in a block to the init of a class, and after some work that class calls me back. The block gets called and most everything works, except when I call anything on "self" within the block. I get Program received signal: “EXC_BAD_ACCESS”. unless I comment out any reference to self...

Closures in JavaScript

I have a code like this where I am trying to add an event handler to some button. I would like to use a global variable & store its current value in callback closure & not its reference. var globalNum="dummy"; function A() { $("#button").click(function() { $("#button").append(globalNum); }); } globalNum="dummyAgain"; Now i...

How can I pass a parameter to the callback of a jQuery ajax request?

I need to pass extra variables to a jQuery, ajax callback function. For example, given: while (K--) { $.get ( "BaseURL" + K, function (zData, K) {ProcessData (zData, K); } ); } function ProcessData (zData, K) { console.log (K); } ProcessData() will report 0 every time (or whatever the last value of K ...

From Eric Lippert's blog: "don't close over the loop variable"

Possible Duplicates: Why is it bad to use a iteration variable in a lambda expression C# - The foreach identifier and closures From Eric Lippert's 28 June 2010 entry: static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) { // base case: IEnumerable<IEnumerable<T>> result = ...

nonlocal keyword in Python 2.x

I'm trying to implement a closure in Python 2.6 and I need to access a nonlocal variable but it seems like this keyword is not available in python 2.x. How should one access nonlocal variables in closures in these versions of python? ...

Function pointers working as closures in C++

Hi, Is there a way in C++ to effectively create a closure which will be a function pointer? I am using the Gnu Scientific Library and I have to create a gsl_function. This function needs to effectively "close" a couple of parameters available when I create it. Is there a nice trick to create a closure so that I don't have to pass all of...

Javascript singleton: access outer attribute from inner function

// i = inner, o = outer, f=function, a=attribute var singleton = function() { var iF = function() { return this.oA; // returns undefined } return { oF: function() { this.oA = true; return iF(); }, oA: false }; }(); singleton.oF(); If singleton were a class (as in a class-based language), shouldn't I be able to acce...

Object oriented JavaScript help

Hi, I have the following code: var HD = function() { }; HD.Car = (function() { var _date = "09/07/2010"; return { Make: undefined, Model: undefined, showMakeAndModel: function() { document.write(this.Make + " " + this.Model + " (data co...

How are the interfaces going to be replaced/augmented by the closures in Java?

Java 7 will have closures ( finally ), and I wonder how the existing code using single method classes/interfaces ( like Runnable, Comparator, etc ) will be used now. Would that code be replaced? Will be a conversion of some sort? An extra method using the closure will be added? Does anyone knows how is this going to work/what the p...

Do PHP closures not have access to parnt function parameters?

Hi, I've been writing some code for PHP 5.3, and I wanted to do something similar to the code I'm showing below. I expect this code to print 'hellohello', but it prints 'hello' instead, and an error. It appears the $inner closure does not have access to the outer function's parameters. Is this normal behavior? Is it a PHP bug? I can't s...

Closures vs Classes?

I have the concept of a "Rule" that I want to be able to process. As such, I created the Interface below: public interface IRule<T> { Boolean IsSatisfiedBy(T value); String GetViolationMessage(T value); } I had planned on creating a series of "Rule" classes to represent the various rules currently supported by the system...

binding and closures groovy

I dont know how to use binding with closures in grooy.I wrote a test code and while running it, it said, missing method setBinding on the closure passed as parameter. void testMeasurement() { prepareData(someClosure) } def someClosure = { assertEquals("apple", a) } void prepareData(testCase) { def binding = new Binding() ...