closures

Passing and storing closures/callbacks in Swift

穿精又带淫゛_ 提交于 2019-12-04 06:18:27
I would like to do the following in swift code: I have to call my api in order to update several items. So I call the api for each item asynchronously. Each api call executes a callback function when it's done. These callbacks decrease a counter, so that when the counter reaches 0 I know that all my api calls are completed. When the counter reaches 0 I would like to call a final callback function (once, when all calls are complete), in order to update my UI and so forth. This final callback is passes to my service in the beginning and stored in a class property for later execution. Executable

Attaching onchange function event to variable

我们两清 提交于 2019-12-04 06:08:13
I recently got into closures and anonymous functions, and I'm wondering if my code is the right way to do it (it works!): newInput.onchange = function(x){ return function(){ PassFileName(x); } }(counter); so this is in a loop that "saves" the current 'counter' value (1,2,3...). If I didn't have the return function, then 'counter' will always be the last value of 'counter'. Am I approaching this correctly with that code? or is there a better way to "capture" the current counter and attach it to an onchange event? thank you! Yes, you're approaching it correctly, but for maximum compatibility

Is this an immediately invoked function expression?

浪尽此生 提交于 2019-12-04 06:04:20
问题 I'm a javascript newbie trying to wrap my mind around this code. I got it here http://brackets.clementng.me/post/24150213014/example-of-a-javascript-closure-settimeout-inside I still have a hard time understanding it. Since it involves some pattern I'm not familiar with. // output 0-9, seperated by 1 sec delay. for (var i = 0; i < 10; i++) { setTimeout(function(x) { return function() { console.log(x); }; }(i), 1000*i); } What is the meaning of (i) in this section of code? function(x) { return

jQuery async ajax query and returning value problem (scope, closure)

爱⌒轻易说出口 提交于 2019-12-04 06:03:23
问题 Code not working because of async query and variable scope problem. I can't understand how to solve this. Change to $.ajax method with async:false - not an option. I know about closures, but how I can implement it here - don't know. I've seen all topics here about closures in js and jQuery async problems - but still nothing. Help, please. Here is the code: var map = null; var marker; var cluster = null; function refreshMap() { var markers = []; var markerImage = new google.maps.MarkerImage('

Storing a closure in a structure — cannot infer an appropriate lifetime

家住魔仙堡 提交于 2019-12-04 06:00:43
I'm trying to implement the State monad in Rust ( State is effectively a wrapper over a function which takes original state and returns modified state and some result). This is how one can implement State in Haskell (monad operations where renamed to unit and bind for sake of simplicity): data State s u = State { run :: s -> (u, s) } -- return unit :: u -> State s u unit u = State $ \s -> (u, s) -- (>>=) bind :: State s u -> (u -> State s a) -> State s a bind m f = State $ \s -> let (u, s') = run m s in run (f u) s' So I try to rewrite it in Rust: pub struct State<'r, S, U> { priv run: 'r |S|

Global or Closure?

六月ゝ 毕业季﹏ 提交于 2019-12-04 05:32:43
问题 As per my understanding variable 'a' is a global variable. But when I check this in Chrome console, I find variable 'a' as a closure. Why? var a = 5; function abc() { var b = 4; return a*b; } console.dir(abc); 回答1: Yes it shows in Chrome but not in Mozilla. First you need to understand lexical scoping in JavaScript. It means that the inner functions have access to the variables and other resources of their parent scope. Here the parent scope is global scope. This is how the function adds a

New closure on scriptblock

£可爱£侵袭症+ 提交于 2019-12-04 05:24:38
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 closure ( { write-host 'i: ' $i }.GetNewClosure() ) value of $i will be tied to this closure. But not in this case. Afer I change the value, write-host takes the new value. On the other side, this works: PS> $i = 1; PS> $action = { write-host 'i: ' $i }.GetNewClosure() PS>

What does it mean to call an escaping closure after the function returns? [duplicate]

会有一股神秘感。 提交于 2019-12-04 05:23:31
问题 This question already has answers here : Escaping Closures in Swift (5 answers) Closed 2 years ago . I was reading the apple developer documentation's definition of escaping closures. It says "a closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns" I am not sure what the last part is supposed to mean, what does it mean by "after the function returns"? Does it mean "after the function returns a value"? 回答1:

JavaScript - referencing 'this' in an inner function

帅比萌擦擦* 提交于 2019-12-04 05:22:42
问题 Consider the following code: MyClass.prototype.my_func = function () { this.x = 10; $.ajax({ // ... success: function (data) { alert(this.x); } }); } It doesn't work, since apparently this is not bound into the closure's execution context. I've been able to work it around by introducing another variable: var _this = this; And this works inside the anonymous function. But looks quite ugly to me. Is there some nice way to handle this? 回答1: This may look like the ugly solution for you and there

global.eval is not able to visit variables in the lexical scope. Does the behavior comply ECMAScript standard?

China☆狼群 提交于 2019-12-04 05:03:11
问题 I have a JavaScript file, e.js var global = Function('return this')(); var i = 1; console.log(eval("100-1")); console.log(eval("i")); console.log(global.eval("100-1")); console.log(global.eval("i")); When I execute it by V8: $ node e.js 99 1 99 undefined:1 i ^ ReferenceError: i is not defined at eval (eval at <anonymous> (/private/tmp/xxxx/e.js:8:20), <anonymous>:1:1) at eval (native) at Object.<anonymous> (/private/tmp/xxxx/e.js:8:20) at Module._compile (module.js:456:26) at Object.Module.