closures

Cast closures/blocks

左心房为你撑大大i 提交于 2019-12-17 15:56:30
问题 In Objective-C, I often pass around blocks. I use them very often to implement patterns that help avoid storing stuff into instance variables, thus avoiding threading/timing issues. For example, I assign them to a CAAnimation via -[CAAnimation setValue:forKey:] so I can execute the block when the animation is finished. (Objective-C can treat blocks as objects; you also can do [someBlock copy] and [someBlock release] .) However, trying to use these patterns in Swift together with Objective-C

understanding the javascript global namespace and closures

北战南征 提交于 2019-12-17 15:34:35
问题 I'm trying to improve my understanding of the global namespace in javascript and I'm curious about a few things: is there a "GOD" (i.e. a parent) object that all objects (since all things except primitives are objects) to answer to and if so would that object be "window" ? why is it bad idea to have vars/functions on a global level? if it is really a bad idea to have vars/functions in global scope then would closures be the best way to avoid this? example: function parent(){ var x = 'some

Python closure not working as expected

荒凉一梦 提交于 2019-12-17 12:03:37
问题 When I run the following script, both lambda's run os.startfile() on the same file -- junk.txt. I would expect each lambda to use the value "f" was set to when the lambda was created. Is there a way to get this to function as I expect? import os def main(): files = [r'C:\_local\test.txt', r'C:\_local\junk.txt'] funcs = [] for f in files: funcs.append(lambda: os.startfile(f)) print funcs funcs[0]() funcs[1]() if __name__ == '__main__': main() 回答1: One way is to do this: def main(): files = [r

How do JavaScript closures work at a low level?

帅比萌擦擦* 提交于 2019-12-17 10:49:53
问题 I understand that a closure is defined as: [A] stack-frame which is not deallocated when the function returns. (as if a 'stack-frame' were malloc'ed instead of being on the stack!) But I do not understand how this answer fits in the context of JavaScript's storage mechanism. How does the interpreter keep track of these values? Is the browser's storage mechanism segmented in a way similar to the Heap and Stack? An answer on this question: How do JavaScript closures work? Explains that: [A]

How to use completionHandler Closure with return in Swift?

一曲冷凌霜 提交于 2019-12-17 10:21:48
问题 I am trying to us a RESTful API that returns some json data. I want to encapsulate the code that creates the HTTP Request and sets the headers in its own method so I can call it by entering a url String and then have the method return a JSON object. In the following snippet of code, I have already created the request object and set the headers, and I call that variable "req". I have not declared any objects named data, response, or error. I have the following code that correctly prints out a

Passing variable in parent scope to callback function

穿精又带淫゛_ 提交于 2019-12-17 10:10:13
问题 This is more of a JavaScript Closure question than a Firebase question. In the following code, the Firebase callback isn't recognizing the variable myArr in the parent scope. function show_fb() { var myArr = []; var firebase = new Firebase('https://scorching-fire-6816.firebaseio.com/'); firebase.on('child_added', function(snapshot) { var newPost = snapshot.val(); myArr.push(newPost.user); console.log(myArr); // works }); console.log(myArr); // doesn't work. myArr in the firebase.on callback

closure inside a for loop - callback with loop variable as parameter [duplicate]

半世苍凉 提交于 2019-12-17 09:59:23
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 2 years ago . I am using jQuery "GET" in a loop to obtain several results from the server. I want to include the loop index as a fixed parameter to the call back but its not working. (I followed the advice of this article on how to do it.) However, the value I get in the call back is completely not what I expect – rather than each loop index value, it is always equal to the

Retrieve String value from function with closure in Swift

社会主义新天地 提交于 2019-12-17 09:57:51
问题 I am trying to retrieve a string value from Firebase in order to get each username with an unique UID that is passed to the function, which returns the username of the user. However - since the firebase ObserveEvent is in closures , I can't return any value back because the actions happens asynchronous (?). I was wondering if it was a way of accomplishing this? The function looks like this: func GetUsername(uid:String) -> String { var username = String() firebase.child("Users").child(uid)

How to fix closure problem in ActionScript 3 (AS3)

给你一囗甜甜゛ 提交于 2019-12-17 09:21:32
问题 In the code below I'm trying to load some images and put them in the stage as soon as they get individually loaded. But it is bugged since only the last image is displayed. I suspect it's a closure problem. How can I fix it? Isn't the behaviour of closures in AS3 the same as in Java Script ? var imageList:Array = new Array(); imageList.push({'src':'image1.jpg'}); imageList.push({'src':'image2.jpg'}); var imagePanel:MovieClip = new MovieClip(); this.addChildAt(imagePanel, 0); for (var i in

How to fix closure problem in ActionScript 3 (AS3)

你说的曾经没有我的故事 提交于 2019-12-17 09:21:30
问题 In the code below I'm trying to load some images and put them in the stage as soon as they get individually loaded. But it is bugged since only the last image is displayed. I suspect it's a closure problem. How can I fix it? Isn't the behaviour of closures in AS3 the same as in Java Script ? var imageList:Array = new Array(); imageList.push({'src':'image1.jpg'}); imageList.push({'src':'image2.jpg'}); var imagePanel:MovieClip = new MovieClip(); this.addChildAt(imagePanel, 0); for (var i in