closures

How does closure work in function expressions passed as parameters?

谁说胖子不能爱 提交于 2019-12-24 08:04:18
问题 A thing about closure . In these two snippets below I'm passing a function expression as a callback. (In the first snippet) When calling back the function expression, I expect to see the anonymous function to close over def , but when I call second() instead of looking inside its "closure" for the variable first (where first is updatedValue ), search and find first in the global variable environment, where first has the value oldValue . function def(first="oldValue" , second=function(){

Laravel Eager Load with dynamic constraints

不想你离开。 提交于 2019-12-24 06:39:26
问题 Can anybody please help me to understand why the following code is working $x = $widget->objGallery->galleryItems()->with(array('captions' => function($query){ $query->where('locale', 'IT' );}))->get() ; but when I am using a dynamic value $id='11'; $x = $widget->objGallery->galleryItems()->with(array('captions' => function($query){ $query->where('locale', $id );}))->get() ; is saying Method Illuminate\View\View::__toString() must not throw an exception 回答1: In fact it's hard to say because

pass variables to an event javascript - no closures, no jq, avoid evals & such

怎甘沉沦 提交于 2019-12-24 05:00:12
问题 ok, this has been addressed by everybody, but yet I feel no closer to understanding what to do. I want to have a loop that sets a bunch of click handlers and have each handler given unique parameters. I'm doing somehting like this now: for (thisThing in things){ myDiv=document.createElement('img'); myDiv.onclick=function(){ // do somehting! // but don't do anything with "thisThing" // because it's got the wrong value in it by the time you call it! // but this function has to have the value of

Groovy closure not capturing static closure variable

偶尔善良 提交于 2019-12-24 04:57:49
问题 Can someone please explain why the call to qux fails? It doesn't seem to capture the name of the static closure variable foo when it is created. If I purposely assign the name to a variable as in baz it works, or if I call it through the class. I thought this variable capture should work for closure class variables too but I must be missing something. class C { static foo = { "foo" } static bar = { C.foo() } static baz = { def f = foo; f() } static qux = { foo() } } println C.foo() //works

swift 3 calling function with completion closure in return

谁都会走 提交于 2019-12-24 04:22:07
问题 I am new at Swift and I am having some trouble. I am trying to get a value from my request. I can print it as closure, but I want to get as string on a VC and work with it, but on function login . I can't figure out how to return from a closure. Function with closure: class Json { var loginToken = "" public func login(userName: String, password: String) -> (Any){ let loginrequest = JsonRequests.loginRequest(userName: userName, password: password) return makeWebServiceCall(urlAddress: URL,

Functions in global context

故事扮演 提交于 2019-12-24 04:21:54
问题 I understand that functions called without the "new" keyword spit out all their properties on to the global context. But I am seeing some curious behavior, with this piece of Javascript code: function Test3() { var a=0; this.inc = function() { return ++a; }; this.noInc = function() { return a; }; this.testRef = function() { return this; }; return { inc: inc, testRef: testRef, noInc: noInc }; } var o = Test3(); // Put func properties on global context var o2 = Test3(); // Put func properties

Passing a closure as a parameter to a constructor c++

↘锁芯ラ 提交于 2019-12-24 02:55:09
问题 In the code below, I'd like to get rid of the weird constructors taking various types of function pointers, and the parameter lists that have to be saved off (along with all the member variables that are required to hold all this), and instead use a closure to do all of that, leaving Event with a single member variable that is something like Closure closure; . Is there a way to do this using closures in C++0x? #include <iostream> #include <list> #include <functional> #include <algorithm>

Return executeSQL function

心已入冬 提交于 2019-12-24 02:43:33
问题 I'm trying to return the results of a SQL query using SQLite. The query works fine and I can output the results inside the executeSql function. But when I try to reach the array from my main function (returnSQLArray) it is undefined. How do I solve this problem? I'm calling returnSQLArray inside another function where I need the results from the query. Code: function returnSQLArray(str) { var db = window.openDatabase("Database", "1.0", "Name", 200000); var result = []; db.transaction(

return a value from closure to main method

China☆狼群 提交于 2019-12-24 02:24:17
问题 I have a closure as follows: public function create(){ DB::transaction(function(){ return 'function called' }); how can I return "function called" from create ? 回答1: this is depends on the implementation of the closure , take this quick example: function transaction($inReturnable, $returnable) { $inReturnable(); return $returnable(); } $value = transaction(function() { return 'hello!'; }, function() { return 'yello!'; }); echo $value; // Output: yello! so, how this could be useful ? -- some

Javascript closures - behavior of overridden functions from the global scope

别来无恙 提交于 2019-12-24 02:18:07
问题 This question is more on javascript principle. function done(){ console.log('done defined with `function done(){ ...`'); } var done = function(){ console.log('done defined with `var done = ...`'); } done = function(){ console.log('without `var`, just `done = ...`'); } If defined right inside <script> tags, will they all do the same thing, right? But if I place them in a closure (function(){ function definintion goes here }()) will any of these three types override either the globally defined