closures

How can I capture the value of an outer variable inside a lambda expression?

江枫思渺然 提交于 2019-12-28 04:07:48
问题 I just encountered the following behavior: for (var i = 0; i < 50; ++i) { Task.Factory.StartNew(() => { Debug.Print("Error: " + i.ToString()); }); } Will result in a series of "Error: x", where most of the x are equal to 50. Similarly: var a = "Before"; var task = new Task(() => Debug.Print("Using value: " + a)); a = "After"; task.Start(); Will result in "Using value: After". This clearly means that the concatenation in the lambda expression does not occur immediately. How is it possible to

Access variables from parent scope in anonymous PHP function

て烟熏妆下的殇ゞ 提交于 2019-12-28 04:01:25
问题 I want to write a function that does some dirty work logging a transaction, but the anonymous function scope does not seem to register the parent scope $db and $value variables. How can I pass the variables into the closure? Ironically, the SO tag 'closures' does not describe the PHP version of it very accurately...? class controller { function submit() { $db = new database(); $result = $db->execute_tx(function() { $db->insert_model_a($value_a); // ERROR: $db is non-object $db->insert_model_b

JavaScript scope and closure

拈花ヽ惹草 提交于 2019-12-27 17:35:14
问题 I'm trying to wrap my head around closures (there's a joke in there somewhere) and I ran across this: (function () { /* do cool stuff */ })(); How does this work? What's the purpose of putting the function in parens? Why the empty parens afterwards? 回答1: The point of this is that any variables declared in the cool stuff will not be created in global namespace. Any function in javascript will create such a scope. Suppose you have some javascript you want to run. If you do this: var b = 1; //

JavaScript scope and closure

时间秒杀一切 提交于 2019-12-27 17:34:58
问题 I'm trying to wrap my head around closures (there's a joke in there somewhere) and I ran across this: (function () { /* do cool stuff */ })(); How does this work? What's the purpose of putting the function in parens? Why the empty parens afterwards? 回答1: The point of this is that any variables declared in the cool stuff will not be created in global namespace. Any function in javascript will create such a scope. Suppose you have some javascript you want to run. If you do this: var b = 1; //

What are Closures and Callbacks?

旧时模样 提交于 2019-12-27 11:39:27
问题 What are closures and callbacks in JavaScript? I've yet to find a good explanation of either. 回答1: Closures have already been well handled in Stackoverflow here is just a selection:- How does a javascript closure work? What exactly does “closure” refer to in JavaScript? can you say this is a right example of Javascript Closure.. Where the places we need to consider avoiding the closures?? JavaScript scope and closure Javascript Closures and ‘this’ context JavaScript - How do I learn about

Assign click handlers in for loop

一世执手 提交于 2019-12-27 08:18:45
问题 I'm having several div's #mydiv1 , #mydiv2 , #mydiv3 , ... and want to assign click handlers to them: $(document).ready(function(){ for(var i = 0; i < 20; i++) { $('#question' + i).click( function(){ alert('you clicked ' + i); }); } }); But instead of showing 'you clicked 3' when click on #mydiv3 (as for every other click) I get 'you clicked 20' . What am I doing wrong? 回答1: It's a common mistake to create closures in loops in Javascript. You need to have some sort of callback function like

How to Bind `$this` to a PHP Function?

我的梦境 提交于 2019-12-25 20:31:12
问题 As of JavaScript, we can do a thing like this to bind a scope to the desired function: function myFunction() { alert(this.foo); } var MyClass = function() { this.foo = 1; }; var c = new MyClass(); myFunction.call(c); // `1` I want to do the same thing using PHP. Currently, what I see working successfully is like this, but this only applies to anonymous functions: $my_function = function() { var_dump($this->foo); }; class MyClass { public $foo = 1; }; $c = new MyClass(); Closure::bind($my

How to Bind `$this` to a PHP Function?

不问归期 提交于 2019-12-25 20:31:09
问题 As of JavaScript, we can do a thing like this to bind a scope to the desired function: function myFunction() { alert(this.foo); } var MyClass = function() { this.foo = 1; }; var c = new MyClass(); myFunction.call(c); // `1` I want to do the same thing using PHP. Currently, what I see working successfully is like this, but this only applies to anonymous functions: $my_function = function() { var_dump($this->foo); }; class MyClass { public $foo = 1; }; $c = new MyClass(); Closure::bind($my

JavaScript reference error can't find variable plus closure blues

安稳与你 提交于 2019-12-25 18:34:41
问题 Just in general I'm getting some errors with this. The major one is in safari and the android browser get the error JavaScript reference error can't find variable The variable in question is b2AABB . Here is the code var worldAABB = new b2AABB(); I am working with box2d JavaScript (the pixel lab variant) for google closure . Also if you can give my closure dependencies and requires a good once over I would appreciate it. <html> <title>Tune Tunnels Alpha</title> <head> <!--====================

What difficulty exactly closure resolved in javascript? [closed]

放肆的年华 提交于 2019-12-25 18:34:27
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . I am working in javaScript from last 3 months, but not able to understand something about closures : What problem exactly closure resolved in this language ? When should I use closure ? Is it related to performance or only case of best practice ? I read How do JavaScript closures work? , which have