closures

nested function memory usage in javascript

隐身守侯 提交于 2019-12-14 01:44:54
问题 I sort of understand closures in javascript, but what I'm not sure about is how it treats nested functions. For example: var a = function(o) { o.someFunction(function(x) { // do stuff }); } I know a new closure is created everytime I call function a , but does that closure also include a new instance of the anonymous function passed to someFunction ? Would it better if I did the ff instead: var b = function(x) { /* do stuff */ } var a = function(o) { o.someFunction(b); } 回答1: In your first

How can I force Python to create a new variable / new scope inside a loop? [duplicate]

心不动则不痛 提交于 2019-12-14 01:15:56
问题 This question already has answers here : Closed 7 years ago . Today I explored a weird behavior of Python. An example: closures = [] for x in [1, 2, 3]: # store `x' in a "new" local variable var = x # store a closure which returns the value of `var' closures.append(lambda: var) for c in closures: print(c()) The above code prints 3 3 3 But I want it to print 1 2 3 I explain this behavior for myself that var is always the same local variable (and python does not create a new one like in other

Calling setTimeout() for all members in object - Never called for 1st member, and called for 2nd member. Why?

早过忘川 提交于 2019-12-13 23:27:39
问题 I have a 3D array (rather JS object), called outerArray in the SSCCE I am posting here in this question, and it contains inner arrays (rather objects), each of which contains several url objects, each of which contains a url field and an interval field. For a given inner array (rather object), the value of interval field for each url will be the same. That is how they have been grouped. My purpose is that , for each inner array, I send an AJAX request periodically at regular intervals, and

Self-executing anonymous functions and closures

你。 提交于 2019-12-13 21:30:25
问题 information I am trying to build a site where I can include certain files and append to my global variable with different methods that will just add easily to the object. Meaning I only need to include the file and this page will now have access to everything in the hutber object. core hutber.js var hutber = {}; (function ($) { "use strict"; //For good development standards :) hutber.init = function(){ }; hutber.init(); })(jQuery); extra bits hutber.form.js (function ($) { "use strict"; //For

How to create generic closures in Swift

孤人 提交于 2019-12-13 15:07:04
问题 I would like to create a function that uses generic closure (block) in swift. My first attempt was to do like : func saveWithCompletionObject(obj : AnyObject, success : AnyObject -> Void, failure : Void -> Void) But as soon as I call this function with another block, such as : func doSomething(success : String -> Void, failure : Void -> Void) { saveWithCompletionObject("Example", success, failure) } I get an error : 'AnyObject' is not a subtype of 'String' Thanks in advance ! 回答1: You cannot

Javascript closures issues

ぃ、小莉子 提交于 2019-12-13 14:23:13
问题 So, I'm still reading Apress Pro Javascript Techniques and i'm having troubles with closures. As John Resig states: Closures allow you to reference variables that exist within the parent function. However it does not provide the value of the variable at the time it is created; It provides the last value of the variable withing the parent function. The most common issue under which you'll see this occurr during a for loop. There is one variable being used as an interaor (e.g., i). Inside of

dispatch_group_leave crash in swift

笑着哭i 提交于 2019-12-13 12:58:41
问题 This happens very rarely. Here is the last line of the stack trace: 0 libdispatch.dylib 0x0000000197a85a9c dispatch_group_leave + 48 dispatch_group_leave is called in a complete closure which is invoked like this: dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in let query = HKStatisticsCollectionQuery(quantityType: quantityType, quantitySamplePredicate: nil, options: statisticOptions, anchorDate: anchorDate, intervalComponents: interval) query

How to access variable from Closure Scope in JavaScript

流过昼夜 提交于 2019-12-13 12:44:09
问题 How to access the closure scope variables in inner function in JavaScript. I want to access UL variable in setTimeout function. ul.find("li").each(function (a, ele) { $(ele).attr("tabindex", options.items[a].tabindex); $(ele).on("focusout", function () { setTimeout(function () { **//ACCESS UL HERE** debugger; }, 1); }.bind(ul,ele)); }.bind(ul)); 回答1: Using closure in setTimeout Closures 'remember' the environment in which they were created. ul.find("li").each(function(a, ele) { $(ele).attr(

Why use a closure for assignment instead of directly assigning a value to a key?

我的未来我决定 提交于 2019-12-13 12:27:26
问题 I was watching this video and at 7:10 he's adding a db dependency and is using a closure to assign the value. My question is why not just use direct assignment instead, I mean isn't doing this: $container['db'] = $capsule; equivalent to doing this: $container['db'] = function ($container) use ($capsule) { return $capsule; } If not, what is the difference and which way is better? 回答1: TLDR it's because defining dependencies as closures makes it possible for dependency injection container to

Attaching onchange function event to variable

我怕爱的太早我们不能终老 提交于 2019-12-13 12:07:57
问题 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