anonymous-function

Where does anonymous function body variables saved ?

不羁岁月 提交于 2019-12-24 13:26:03
问题 Below code is working but why ? Where does the x and y coming/saved when I invoke anonymous method in the loop. thanks static void Main(string[] args) { int x=1; int y=2; var dic = GetDic(x, y); for (int i = 0; i < 5;i++ ) { System.Console.WriteLine(dic[i].Invoke().ToString()); } } private static Dictionary<int, Func<int>> GetDic(int x, int y) { var dic = new Dictionary<int, Func<int>>() { {0,()=>{return y;}}, {1,()=>{return x;}}, {2,()=>{return x+y;}}, {3,()=>{return x-y;}}, {4,()=>{return y

jQuery: Rewriting Anonymous Callback to a Named Function

独自空忆成欢 提交于 2019-12-24 13:03:51
问题 If I do this: $('h1').slideUp('slow', function() { $('div:first').fadeOut(); }); h1 will slide up, then the first div will fade out. However, if I do this: function last() { $('div:first').fadeOut(); } $('h1').slideUp('slow', last()); h1 will slide up and div will fade out at the same time! How can I make my second example work the same as the first one, where fadeOut() is called AFTER slideUp()? 回答1: You don't need to use the function return value (which you get by calling the function), but

Do you name your Anonymous Function in a Function Expression?

坚强是说给别人听的谎言 提交于 2019-12-24 08:44:02
问题 I'm using firebug here, and trying to write a blog post to demonstrate something just like these code. // Unnamed Anonymous Function var count1 = function () { var x = 0, f; f = function () { x = x + 1; return x; }; return f; }; // Named Anonymous Function var count2 = function cf() { var x = 0, f; f = function ff() { x = x + 1; return x; }; return f; }; var c = count1(); console.log(count1); // function() console.log(c); // function() var d = count2(); console.log(count2); // cf() console

map function in Scala

╄→尐↘猪︶ㄣ 提交于 2019-12-24 07:39:52
问题 In Scala programming use an anonymous function is a usual thing . when i decide to creat a vector as out put of an anonymous function from two different ways way one : var hold1=(1 to 5).map(_*2) way two: var hold2=(1 to 5).map(2*) I want to know what is the difference between those two declaration ? 回答1: In short - they are exactly the same. First approach: var hold1 = (1 to 5).map(_*2) Let's rewrite this another way to demonstrate what's really happening under the hood (no syntactic sugar)

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

Setting anonymous function scope of 'this' in jQuery utility / ajax methods

让人想犯罪 __ 提交于 2019-12-24 01:27:20
问题 As noted in this blog post you can set the scope of this in an anonymous function in Javascript. Is there a more elegant way of scoping this in the anonymous function call on success of the AJAX request (i.e. not using that )? For example: var Foo = { bar: function(id) { var that = this; $.ajax({ url: "www.somedomain.com/ajax_handler", success: function(data) { that._updateDiv(id, data); } }); }, _updateDiv: function(id, data) { $(id).innerHTML = data; } }; var foo = new Foo; foo.bar('mydiv')

javascript anonymous function syntax

前提是你 提交于 2019-12-23 17:07:38
问题 What is the difference between the following two blocks? // block 1 { console.log("anonymous block"); } // block 2 (function anon() { console.log("anonymous block 2"); })(); I ran this in Netbeans (using the node.js plugin) and they both seem to work... 回答1: The difference is that you can use the latter form to hide global variables without destroying them. For example, suppose you're using the jQuery library, which by default aliases its main namespace to $ . If you wanted to use $ for a

Is it possible to have a recursive anonymous function in MATLAB? [duplicate]

核能气质少年 提交于 2019-12-23 16:24:15
问题 This question already has an answer here : Recursive Anonymous Function Matlab (1 answer) Closed last year . I repeatedly want to apply a function, using a past output as the new input. For readability (I'm writing from a mathematics perspective, not a programmer's perspective), I would like to define it as a simple anonymous function instead of a full function block. So, instead of something like function f=myfun(x,n) if n>1 f=myfun(myfun(x,n-1),1); else f=expression(x); end end I would like

Using final 1-element array for anonymous inner class

一个人想着一个人 提交于 2019-12-23 09:16:22
问题 I stumbled across this trick for getting a value from an anonymous inner class to a variable which is declared in the outer class. It works, but it feels like a dirty hack: private int showDialog() { final int[] myValue = new int[1]; JPanel panel = new JPanel(); final JDialog dialog = new JDialog(mainWindow, "Hit the button", true); dialog.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE ); JButton button = new JButton("Hit me!"); button.addActionListener(new ActionListener() {

What do PHP closures return in IF statements?

两盒软妹~` 提交于 2019-12-23 08:29:10
问题 My goal is to put some complex logic in an if() statement. Let's say I have an array of values and I'm going to execute a bit of code if everything in my array is nonzero. Normally, I could say, $valid = true , foreach my array, and set $valid = false when a zero is found. Then I'd run my code if ($valid) . Alternatively, I could put my loop into a function and put the function intop my if() . But I'm lazy, so I'd rather not muck about with a bunch of "valid" flags, and I'd rather not write a