closures

Accessing outside variable using anonymous function as params

走远了吗. 提交于 2019-12-17 02:35:16
问题 Basically I use this handy function to processing db rows (close an eye on PDO and/or other stuff) function fetch($query,$func) { $query = mysql_query($query); while($r = mysql_fetch_assoc($query)) { $func($r); } } With this function I can simply do: fetch("SELECT title FROM tbl", function($r){ //> $r['title'] contains the title }); Let's say now I need to concatenate all $r['title'] in a var (this is just an example). How could I do that? I was thinking something like this, but it's not very

Is it true that every function in JavaScript is a closure?

妖精的绣舞 提交于 2019-12-17 02:32:07
问题 I understand that every function in JavaScript is a first-class object and it has an internal property [[scope]] which hosts the binding records of the function's free variables. However, there are two special cases. Is the function created by Function constructor also a closure? The function object created by Function constructor is special, because its [[scope]] may not refer to the lexical environments of its outer functions, but only the global context. For example, var a = 1; var fn =

(…()) vs. (…)() in javascript closures [duplicate]

戏子无情 提交于 2019-12-17 02:09:29
问题 This question already has answers here : Location of parenthesis for auto-executing anonymous JavaScript functions? (4 answers) Closed 4 years ago . I know this is silly, but there's any difference between this: (function() { var foo = 'bar'; })(); and this? (function() { var foo = 'bar'; }()); JSLint tells us to Move the invocation into the parens that contain the function , but I see no need to. Edit: The answers are too cool. ~function , the JSHint alternative along with jQuery's

What is this JavaScript pattern called and why is it used?

梦想的初衷 提交于 2019-12-17 01:39:31
问题 I'm studying THREE.js and noticed a pattern where functions are defined like so: var foo = ( function () { var bar = new Bar(); return function ( ) { //actual logic using bar from above. //return result; }; }()); (Example see raycast method here). The normal variation of such a method would look like this: var foo = function () { var bar = new Bar(); //actual logic. //return result; }; Comparing the first version to the normal variation, the first seems to differ in that: It assigns the

Memory leak risk in JavaScript closures

对着背影说爱祢 提交于 2019-12-16 20:15:42
问题 Solved There's a lot of contradictory information on the web, regarding this subject. Thanks to @John, I managed to work out that the closures (as used below) aren't the cause of memory leaks, and that -even in IE8- they're not that common as people claim. In fact there was only 1 leak that occurred in my code, which proved not that difficult to fix. From now on, my answer to this question will be: AFAIK, the only time IE8 leaks, is when events are attached/handlers are set on the global

The foreach identifier and closures

与世无争的帅哥 提交于 2019-12-16 19:50:10
问题 In the two following snippets, is the first one safe or must you do the second one? By safe I mean is each thread guaranteed to call the method on the Foo from the same loop iteration in which the thread was created? Or must you copy the reference to a new variable "local" to each iteration of the loop? var threads = new List<Thread>(); foreach (Foo f in ListOfFoo) { Thread thread = new Thread(() => f.DoSomething()); threads.Add(thread); thread.Start(); } - var threads = new List<Thread>();

Private variables and closures

你。 提交于 2019-12-14 03:57:29
问题 As Douglas Crockford says we can have private properties using closures in JavaScript and they are very handy to handle secure data. Now I understand the concept of Encapsulation, as it helps us to manage and structure the code . Even private variables are useful for performance, eg: you can cache DOM elements, properties and global variables for iterative access. So the question is: How exactly closures or private variables help us in handling the sensitive data? 回答1: You can secure the data

Loops and closures. For and Var

时光总嘲笑我的痴心妄想 提交于 2019-12-14 03:05:46
问题 I found many topics explaining this problem, on how I can fix the following code by using var, like this one http://conceptf1.blogspot.com/2013/11/javascript-closures.html or this one JavaScript closure inside loops – simple practical example. But I really can't understand why it is not working when using var and working when using let. var funcs = []; for (var i = 0; i < 3; i++) { // let's create 3 functions funcs[i] = function() { // and store them in funcs console.log("My value: " + i); //

How to return values from Haneke's async fetch method

為{幸葍}努か 提交于 2019-12-14 02:44:42
问题 I'm trying to parse some data that I cached using Haneke Swift . I've cached the data and have written the parser to accomplish this. This parser is in a separate class called AssembleCalendar() . Using Haneke's example code for fetching, I've tried with complete and utter failure to actually return a value from the closure. My attempt func getScheduledItems() -> [ScheduledItem] { var scheduledItem = [ScheduledItem]() // initialize array let cache = Shared.dataCache cache.fetch(key:

SICP Section 3.1.1 - Local state in procedures seems inconsistent

╄→гoц情女王★ 提交于 2019-12-14 02:18:54
问题 I am working my way through SICP. I am on Section 3.1.1 and looking at local state. I am evaluating these exercises in GNU Guile v2.0.11. I did find a similar question about this section, but it seems not to address the point I am struggling with, or I am overly obtuse. The two examples I am looking at are these: (define new-withdraw (let ((balance 100)) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))) (define (make-withdraw