anonymous-function

JavaScript - referencing 'this' in an inner function

帅比萌擦擦* 提交于 2019-12-04 05:22:42
问题 Consider the following code: MyClass.prototype.my_func = function () { this.x = 10; $.ajax({ // ... success: function (data) { alert(this.x); } }); } It doesn't work, since apparently this is not bound into the closure's execution context. I've been able to work it around by introducing another variable: var _this = this; And this works inside the anonymous function. But looks quite ugly to me. Is there some nice way to handle this? 回答1: This may look like the ugly solution for you and there

Serializing anonymous functions in php

前提是你 提交于 2019-12-04 05:14:52
is there any way to serialize an anonymous function in php? i have found this http://www.htmlist.com/development/extending-php-5-3-closures-with-serialization-and-reflection/ protected function _fetchCode() { // Open file and seek to the first line of the closure $file = new SplFileObject($this->reflection->getFileName()); $file->seek($this->reflection->getStartLine()-1); // Retrieve all of the lines that contain code for the closure $code = ''; while ($file->key() < $this->reflection->getEndLine()) { $code .= $file->current(); $file->next(); } // Only keep the code defining that closure

Do you need to “unwire” an anonymous function/lambda

萝らか妹 提交于 2019-12-04 03:47:56
My understanding is that any event handlers wired up in C# need to be unwired as such. Object myObject = new Object(); myObject.Event += EventHandler; //Wired myObject.Event -= EventHandler; //Unwired But do you need to unwire the following code? and if so, how? Object myObject = new Object(); myObject.Event += (object sender, EventArgs e) => { }; //Wired myObject.Event -= ????? //Unwire? How? My assumption is no? Daniel Hilgarth Yes, you need to (*) and you need to do it like this: Object myObject = new Object(); EventHandler handler = (object sender, EventArgs e) => { }; myObject.Event +=

Scala closures compared to Java innerclasses -> final VS var

南楼画角 提交于 2019-12-04 03:12:51
问题 I've first asked this question about the use of final with anonymous inner classes in Java: Why do we use final keyword with anonymous inner classes? I'm actually reading the Scala book of Martin Odersky. It seems Scala simplifies a lot of Java code, but for Scala closures I could notice a significant difference. While in Java we "simulate" closures with an anonymous inner class, capturing a final variable (which will be copied to live on the heap instead of the stack) , it seems in Scala we

Break array_walk from anonymous function

亡梦爱人 提交于 2019-12-04 02:48:47
问题 Is there a way to stop an array_walk from inside the anonymous function ? Here is some sample code (that works) to show what I mean, that checks if an array has only numeric values. $valid = true; array_walk($parent, function ($value) use (&$valid) { if (!is_numeric($value)) { $valid = false; } }); return $valid ? 'Valid' : 'Invalid'; If I have a big enough array, and the first entry is invalid, the rest of the (redundant) checks are still done, so I would like to stop the execution. Using

JS: What's the difference between a ! closure and () closure? [duplicate]

大城市里の小女人 提交于 2019-12-04 02:16:01
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: What does the exclamation mark do before the function? So I was going back and looking over some of my own code as well as some other javascript code and I realized that a while back when I started writing javascript libraries I was using closures that looked something like this: (function( window, document, undefined ) { // code })( window, document ); But then I saw some bootstrap code and I changed to this

PHP: Pass anonymous function as argument

為{幸葍}努か 提交于 2019-12-04 00:53:19
Is it possible to pass an anonymous function as an argument, and have it execute immediately, thus passing the function's return value? function myFunction(Array $data){ print_r($data); } myFunction(function(){ $data = array( 'fruit' => 'apple', 'vegetable' => 'broccoli', 'other' => 'canned soup'); return $data; }); This throws an error due to the Array type-hint, complaining of an object being passed. Alright, if I remove the type-hint, it of course spits out Closure Object , rather than the results I want. I understand that I am technically passing an object instance of Closure to myFunction

Matlab - for loop in anonymus function

╄→尐↘猪︶ㄣ 提交于 2019-12-04 00:34:07
I'm quite new to matlab, but I know how to do both for loops and anonymous functions. Now I would like to combine these. I want to write: sa = @(c) for i = 1:numel(biscs{c}), figure(i), imshow(biscs{c}{i}.Image), end; But that isn't valid, since matlab seem to want newlines as only command-seperator. My code written in a clear way would be (without function header): for i = 1:numel(biscs{c}) figure(i) imshow(biscs{c}{i}.Image) end I look for a solution where either I can write it with an anonymous function in a single line like my first example. I would also be happy if I could create that

Anonymous function with a variable-length argument list

末鹿安然 提交于 2019-12-04 00:33:43
Can I create an anonymous function that accepts a variable number of arguments? I have a struct array S with a certain field, say, bar , and I want to pass all the bar values to my anonymous function foo . Since the number of elements in struct S is unknown, foo must be able to accept a variable number of arguments. The closest thing that I've been able to come up with is passing a cell array as the input argument list: foo({arg1, arg2, arg3, ...}) and I'm invoking it with foo({S.bar}) , but it looks very awkward. Creating a special m-file just for that seems like an overkill. Any other ideas?

Anonymous function C++

戏子无情 提交于 2019-12-03 23:28:38
I am trying to use the function signal(int,void(*)(int)) from <csignal> to handle the floating point exception SIGFPE. I'd like to be able to print some useful diagnostics besides just a message saying "Floating point exception" or something to that effect. This means the function I pass as the handler to signal needs access to some of the data in my code. Therein lies the rub. The function must return void and accept only 1 parameter of type int . I cannot make the handler a member function of my data storage class since then the type would be void(Foo::*)(int) due to the hidden this pointer.