closures

writing DSL map inside map with closure groovy,

孤人 提交于 2019-12-11 15:37:56
问题 Accessing map inside map with closure, I have a map object the values is another map object e.g:- ` to access the data like this I can issue def map = [name:"Gromit", likes:"cheese", id:1234] def map2 =[map1:map] map2.each{entry -> println entry.key entry.value.each {entry1 -> println entry1.key println entry1.value } } to access a single map i can issue map.each{entry -> println entry.key println entry.value } ' How can I write a DSL for the above map example in simple any hint? 回答1: Here is

Assign click handlers in for loop

对着背影说爱祢 提交于 2019-12-11 15:32:48
问题 I'm having several div's #mydiv1 , #mydiv2 , #mydiv3 , ... and want to assign click handlers to them: $(document).ready(function(){ for(var i = 0; i < 20; i++) { $('#question' + i).click( function(){ alert('you clicked ' + i); }); } }); But instead of showing 'you clicked 3' when click on #mydiv3 (as for every other click) I get 'you clicked 20' . What am I doing wrong? 回答1: It's a common mistake to create closures in loops in Javascript. You need to have some sort of callback function like

Callback definition closure issue

眉间皱痕 提交于 2019-12-11 14:55:01
问题 I run into a bit of confusion regarding scoping with respect to where a callback is defined . function test(){ var b = 3 var fun = function(){ var printingFunction = function(){ console.log(b) } printingFunction() } return fun } test()() //prints 3, as expected because of closures However, the following doesnt work function test(){ var b = 3 var fun = function(cb){ cb() } return fun } test()(function(){console.log(b)}) //b is not defined I would expect that since the function is passed as an

What can javascript closures be used for?

人走茶凉 提交于 2019-12-11 13:43:58
问题 I've been wondering for a while - what can JavaScript closures be used for? I know how to write them and I know how they work, but I just can't get the idea of how they can be used. function closure() { var name = "John"; function displayName() { return 'Hello, ' + name; } return displayName; } So far I only found one use for them - encapsulating data, so it won't be visible outside the function. But what else can they be used for? Should I write OOP with closures? Do I need to put all my

How can I gain access to a Javascript variable from within a return function?

☆樱花仙子☆ 提交于 2019-12-11 13:24:36
问题 function storyData($http, $q) { var URL = 'stories.json'; var storyCache = []; this.getStories = function() { var deferred = $q.defer(); alert(URL); $http({method: 'GET', url: URL}) .success(function (data, status, headers, config) { storyCache = data; alert(storyCache); //correct data appears in the alert window, but this doesn't set the instance storyCache variable for some reason deferred.resolve(data); }) .error(function (data, status, headers, config) { deferred.reject(status); });

Undefined Variable Inside Mail::send in Laravel 5

走远了吗. 提交于 2019-12-11 13:02:15
问题 public function postAcceptedSign($md5) { $notif = CustomerOrder::where('sign_link', '=', $md5)->get(); Mail::send('emails.signed-notif', ['order_num' => $notif[0]->order_number], function ($m) { $m->to('mis@qdf-phils.com', '')->subject('Customer Order '.$notif[0]->order_number.' is now signed'); }); Session::flash('alert-success', 'Order Signed.'); return Redirect::to('home'); } I am getting Undefined variable: notif which points to this Mail::send('emails.signed-notif', ['order_num' =>

Closure/callback in for loop [duplicate]

南楼画角 提交于 2019-12-11 13:01:32
问题 This question already has an answer here : Closed 8 years ago . Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I have been trying to get this to work for a while and decided to just ask. I have the following: function doSomething(var1, var2) { dojo.xhrGet({ url:"somUrl", content: {value1:var1,value2:var2}, preventCache: true, handleAs:"json", load: function(response){ for(var i in response.myObject) { var listItem = new dojox.mobile.ListItem({ label: response

What does “access to disposed closure” mean here?

*爱你&永不变心* 提交于 2019-12-11 12:07:11
问题 Why does Resharper warn me about "access to disposed closure" in the following code sample: using (SqlCommand command = new SqlCommand()) { command.Parameters.Add("@temp", SqlDbType.VarChar); Action<string> action = str => { command.Parameters["@temp"].Value = string.Empty; }; } I don't use delegate outside using statement... How to fix this? 回答1: You are referencing command in action , you can use action in somewhere else then in using and reference to disposed command . Resharper is telling

How can I write a JS callback function that remembers the old value of a changing variable? [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-11 11:18:03
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 5 years ago . Here's a simplification of my JS program. mylist is an array of strings and MyAsync is a function that takes an object and arranges for the supplied callback to be called at a later time. for (var myindex = 0; myindex < mylist.length; myindex += 1) { MyAsync(mylist[myindex], function () { alert(myindex); }); } When there's ten items in mylist , the result is

accessing variables in javascript closures

白昼怎懂夜的黑 提交于 2019-12-11 11:07:10
问题 var add = (function () { var counter = 0; return function () { var reset = function() { counter = 0; } return counter += 1; } })(); This is a self-invoking function that creates a "private" variable. How would I create a function reset that will reset the counter to 0? I've tried declaring the function inside the closure, but when I call it using add.reset(), it tells me this method is undefined. Any help in understanding is appreciated! 来源: https://stackoverflow.com/questions/31642931