closures

Weak self in closures and consequences example

試著忘記壹切 提交于 2019-12-03 13:07:36
I have done abit of research on stackoverflow and apple's documentation about ARC and Weak/Unowned self ( Shall we always use [unowned self] inside closure in Swift ). I get the basic idea about strong reference cycle and how it is not good as they cause memory leaks. However, I am trying to get my head around when to use Weak/Unowned self in closures. Rather then going into the "theory", I think it would really really help if someone could kindly explain them in terms of the bottom three cases that I have. My questions is Is it OK to put weak self in all of them (I think for case two there is

Groovy: meaning of 'this' inside a closure

怎甘沉沦 提交于 2019-12-03 12:49:54
The following example is adapted from 'Groovy in Action' class Mother { Closure birth() { def closure = { caller -> [this, caller] } return closure } } Mother julia = new Mother() closure = julia.birth() context = closure.call(this) println context[0].class.name // Will print the name of the Script class assert context[1] instanceof Script According to the book, the value of this inside the closure is the outermost scope (i.e. the scope in which julia is declared). Am I right in assuming that this inside a closure evaluates to the scope in which the closure is called? within the closure shown

Why don't all of these variables get treated the same way?

痴心易碎 提交于 2019-12-03 12:28:19
I was checking that the position of variable declarations in VB.NET don't matter, except for scope, (for this question ) and I thought I better check what happens when they're "lifted" into a closure. I haven't read the spec, but I can't explain these results: Dim outer As Integer For i = 1 To 2 Dim inner As Integer Try Dim inner2 As Integer Do Dim inner3 As Integer Call Sub() Dim inner4 As Integer Console.WriteLine(outer & ", " & inner & ", " & inner2 & ", " & inner3 & ", " & inner4) outer = i inner = i inner2 = i inner3 = i inner4 = i End Sub() Loop Until True Finally End Try Next The above

addEventListener, for(), index. how to use closure? [duplicate]

送分小仙女□ 提交于 2019-12-03 12:13:55
This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Javascript infamous Loop issue? [duplicate] (5 answers) I have this code: var items = this.llistat.getElementsByTagName('a'); for( var i = 0; i < items.length; i++ ){ items[i].addEventListener('click', function(event) { alert( i ); }, items[i]); } where the event is listened, but there are 3 items and the alert allways print 3 on any of the elements (it doesn't respect the index), Dosen't items[i] shouldn't do the job as closure? thanks! That's a classical closure issue : you must

Wait to return from a javascript function until condition met

﹥>﹥吖頭↗ 提交于 2019-12-03 12:02:21
This is an odd problem. I have a client object that I am building up using Crockford-esque public/private members: var client = function() { var that, remote_data, other_data; // add public interface that.doStuff = function(){...} // wait for remote resources to load remote_data = jsonRequest1(); other_data = jsonRequest2(); return that; }; The problem I'm having is that I need to load some remote JSON resources prior to returning the new 'that' object (which signals a ready client). Data is returned asynchronously (obviously), and I am setting boolean variables to indicate when each remote

Extra parentheses on function [duplicate]

与世无争的帅哥 提交于 2019-12-03 11:54:45
Possible Duplicate: What do parentheses surrounding a JavaScript object/function/class declaration mean? What does this “(function(){});”, a function inside brackets, mean in javascript? A Javascript function I encountered markup similar to this: var something = (function(){ //do stuff return stuff; })() document.ondblclick = function(e) { alert(something(e)) }; I don't understand the opening ( and closing )() in the something variable. Could you explain the difference to writing it like this? var something = function(){ //do stuff return stuff; }; Thanks! It's probably easier to understand if

Groovy - closures vs methods - the difference

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 11:37:50
If you look very carefully at the picture included, you will notice that you can refactor Groovy code using the Eclipse IDE and convert a method to a closure and vice versa. So, what exactly is a closure again and how is it different than a method? Can someone give a good example of using a closure as well as why it's a useful feature? Anonymous inner classes weren't good enough? Seagull Closure is a Closure class instance , that implements Call logic. It may be passed as argument or assigned to a variable. It also has some logic concerned with scope variable accessing and delegating calls.

How does Scala maintains the values of variable when the closure was defined?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 11:33:59
问题 Does scala maintains the values of variable by copy or reference? For example, in Ruby "the closure will actually extend the lifetime of all the variables that it needs. It will not copy them, but will retain a reference to them and the variables themselves will not be eligible for garbage collection (if the language has garbage collection) while the closure is around". [SKORKIN] 回答1: Closures in Scala also don't deep copy objects, they'll only keep a reference to the object. Moreover, a

What is the point of OOP visibility in PHP when Closures and Reflections are available?

我是研究僧i 提交于 2019-12-03 11:28:01
Consider this code here: final class TinkerWithMe { protected $key1 = 19; private $key2 = 88; } $class = new TinkerWithMe(); $getKeys = function() { return array($this->key1, $this->key2); }; $oldKeys = $getKeys->call($class); $newKey1 = 96; $newKey2 = 42; $setKeys = function() use ($newKey1, $newKey2) { $this->key1 = $newKey1; $this->key2 = $newKey2; }; $setKeys->call($class); Why bother with OOP visibility when you can get around it so easily with Closures or Reflections? Is there a way of blocking this kind of thing that I am missing? Visibility modifiers aren't iron clad protection or

Please explain this Javascript closure exercise [duplicate]

人走茶凉 提交于 2019-12-03 10:45:49
问题 This question already has answers here : How do JavaScript closures work? (86 answers) Closed 5 years ago . I'm a javascript noob trying to wrap my head around the closure exercise below. Now, I know the result is 122. Can anyone walk me through this step by step (what gets passed to what), so I can understand how closures work? var hidden = mystery(3); var jumble = mystery3(hidden); var result = jumble(2); function mystery ( input ){ var secret = 4; input+=2; function mystery2 ( multiplier )