closures

For-loop saving state with closure

依然范特西╮ 提交于 2019-12-07 02:45:40
问题 Forgive me if this might be a bit of a noobie question, but this should work shouldn't it? var elems = [1,2,3,4,5] for (var i = 0; i <elems.length; i++) { return (function(e){ console.log(e) })(i); } Meaning, it should spit out >>node file.js 1 2 3 4 5 For some reason this isn't doing this. Rather when it is run in terminal, it spits out >>node file.js 1 What am I missing? Could you please elaborate. 回答1: Because you are returning the value returned by the IIFE immediately, in this statement

Javascript Closures Explanation [closed]

蓝咒 提交于 2019-12-07 02:34:09
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 9 years ago . Question: There seem to be many benefits to Closures, but what are the negatives? Additionally, is my understanding of Closures correct? Finally, once closures are created, can they be destroyed? I've been

Anonymous function declaration shorthand javascript

白昼怎懂夜的黑 提交于 2019-12-07 01:57:10
问题 I'm wondering whether there is any way to shorten anonymous function declaration in JavaScript through the utilization of preprocessor/compiler like Google Closure. I figure it'd be quite neat for callbacks. For example, normally I'd write a qunit test case this way: test("Dummy test", function(){ ok( a == b );}); I'm looking for some Clojure-inspired syntax as followed: test("Dummy test", #(ok a b)); Is it possible? 回答1: Without worrying about preprocessors or compilers, you could do the

How to access variable from Closure Scope in JavaScript

放肆的年华 提交于 2019-12-07 01:53:28
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)); Using closure in setTimeout Closures 'remember' the environment in which they were created. ul.find("li").each(function(a, ele) { $(ele).attr("tabindex", options.items[a].tabindex); $(ele).on("focusout", function() { setTimeout(function(ul) { return

Command failed due to signal: Segmentation fault: 11 while emitting IR SIL function

假如想象 提交于 2019-12-06 23:55:14
问题 I want to add closure properties in the extension of UITextView so I define a closure using typealias: typealias TextViewHeightDidChangedClosure = (_ currentTextViewHeight:CGFloat)->Void extension UITextView{ func setTextViewHeightDidChanged(textViewHeightDidChanged:TextViewHeightDidChangedBlock){ objc_setAssociatedObject(self, &TextViewHeightDidChangedBlockKey, textViewHeightDidChanged, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC) } func textViewHeightDidChanged()-

Question on Scala Closure (From “Programming in Scala”)

梦想的初衷 提交于 2019-12-06 23:49:17
问题 I don't understand why authors said that Code Listing 9.1 from "Programming in Scala" use closure. In chapter 9, they show how to refactor code into more less duplicated form, from this original code: object FileMatcher { private def filesHere = (new java.io.File(".")).listFiles def filesEnding(query: String) = for (file <- filesHere; if file.getName.endsWith(query)) yield file def filesContaining(query: String) = for (file <- filesHere; if file.getName.contains(query)) yield file def

Javascript setTimeout in foreach: need help creating a closure

大兔子大兔子 提交于 2019-12-06 23:27:52
问题 I have this function notes.forEach(function(note) { setTimeout(function() { playNote(note); }, 1000); }); This doesn't work. It plays all the notes at the same time, instead of playing them sequentially with a 1 second gap in between. It looks like I need to have a closure here to make this work. Could someone help me fix this function so it would play the note with the delay between each note? 回答1: because all timeouts are set at the same time... Do something like this: playAllNotes(0);

Javascript closures - variables vs parameters

▼魔方 西西 提交于 2019-12-06 20:41:30
问题 I'm trying to learn Javascript closures. I'm having trouble getting my head around the fact that when you create several closures in a loop, all closures save only the last state of a variable. With this example var links = document.getElementsByTagName('a'); for (var x=0; x<links.length; x++) attachListener(); function attachListener() { links[x].addEventListener('click', function(){ console.log(x); }, false); }; When I have three links in my document, clicking on any link shows "3", I guess

View a PHP Closure's Source

好久不见. 提交于 2019-12-06 19:21:02
问题 Is it possible to reflect into or otherwise view the source of a PHP closure object? That is, if I do something like this $closure = function() { return 'Hi There'; }; and then something like this var_dump($closure); PHP outputs object(Closure)[14] That is, I know the object's a closure, but I have no idea what it does. I'm looking for a reflection method, function, or debugging extension that will allow me to dump the actual body of anonymous function. 回答1: What you can get from PHP is

Reusing part of Grails criteria closure

我的梦境 提交于 2019-12-06 19:06:34
问题 I have a fairly large criteria closure in my Grails application, and I would like to reuse part of it in several places in my application. Rather than duplicating the section I need to reuse, I'd like to define this as a separate closure and reference it wherever it is needed, but I am struggling a bit with the syntax. This is a simplified / cut down version, but essentially my criteria looks something like this: def criteriaClosure = { and { // filtering criteria that I'd like to reuse in