Anonymous functions use a block of code as a value, defining it as an inline function without a name.
Questions tagged [anonymous-function]
2121 questions
95
votes
2 answers
How can I pass a reference to a function, with parameters?
Possible Duplicate:
How can I pre-set arguments in JavaScript function call? (Partial Function Application)
I need to able to pass a reference to a function with a given set of parameters.
Here is an example of passing a reference without…

Andreas Grech
- 105,982
- 98
- 297
- 360
94
votes
2 answers
How to call a closure that is a class variable?
class MyClass {
var $lambda;
function __construct() {
$this->lambda = function() {echo 'hello world';};
// no errors here, so I assume that this is legal
}
}
$myInstance = new MyClass();
$myInstance->lambda();
//Fatal error: Call to…

rsk82
- 28,217
- 50
- 150
- 240
93
votes
5 answers
How to removeEventListener that is addEventListener with anonymous function?
function doSomethingWith(param)
{
document.body.addEventListener(
'scroll',
function()
{
document.write(param);
},
false
); // An event that I want to remove later
}
setTimeout(
…

Japboy
- 2,699
- 5
- 27
- 28
91
votes
5 answers
Using `$this` in an anonymous function in PHP pre 5.4.0
The PHP manual states
It is not possible to use $this from anonymous function before PHP
5.4.0
on the anonymous functions page. But I have found I can make it work by assigning $this to a variable and passing the variable to a use statement at…

steampowered
- 11,809
- 12
- 78
- 98
85
votes
4 answers
Anonymous function shorthand
There's something I don't understand about anonymous functions using the short notation #(..)
The following works:
REPL> ((fn [s] s) "Eh")
"Eh"
But this doesn't:
REPL> (#(%) "Eh")
This works:
REPL> (#(str %) "Eh")
"Eh"
What I don't understand…

Cedric Martin
- 5,945
- 4
- 34
- 66
82
votes
3 answers
Use keyword in functions - PHP
Possible Duplicate:
In Php 5.3.0 what is the Function “Use” Identifier ? Should a sane programmer use it?
I've been examining the Closures in PHP and this is what took my attention:
public function getTotal($tax)
{
$total = 0.00;
…

Tarik
- 79,711
- 83
- 236
- 349
78
votes
1 answer
Closure vs Anonymous function (difference?)
Possible Duplicates:
What is Closures/Lambda in PHP or Javascript in layman terms?
What is the difference between a 'closure' and a 'lambda'?
Hi,
I have been unable to find a definition that clearly explains the differences between a closure and…

Maxim Gershkovich
- 45,951
- 44
- 147
- 243
76
votes
6 answers
What are the benefits to using anonymous functions instead of named functions for callbacks and parameters in JavaScript event code?
I'm new-ish to JavaScript. I understand many of the concepts of the language, I've been reading up on the prototype inheritance model, and I'm whetting my whistle with more and more interactive front-end stuff. It's an interesting language, but…

Doug Stephen
- 7,181
- 1
- 38
- 46
72
votes
4 answers
Javascript anonymous function call
I was reading JS sources from Twitter — on my way to improve my JS knowledge base, when I came across the strange way of calling anonymous function:
!function( $ ) {
...
}( window.jQuery );
... and this works! :)
It's obvious to everyone, that…

Konstantin Likhter
- 910
- 1
- 6
- 8
70
votes
2 answers
Callback function using variables calculated outside of it
Basically I'd like to do something like this:
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$avg = array_sum($arr) / count($arr);
$callback = function($val){ return $val < $avg };
return array_filter($arr, $callback);
Is this actually possible?…

Breno Gazzola
- 2,092
- 1
- 16
- 36
65
votes
5 answers
Javascript 'colon' for labeling anonymous functions?
What does this code refer too?
queryString: function() {
//some code
}
I tested it in the WebConsole (Firefox) but it wouldn't execute, so I'm thinking that it isn't equivalent to function queryString() {}.
So what is it exactly?

knownasilya
- 5,998
- 4
- 36
- 59
64
votes
1 answer
Is it possible to set a breakpoint in anonymous functions?
I quickly want to determine whether/when a set of events are triggered. Therefore I quickly assigned empty lambda's to them.
_session.TopologyStarted += () => { };
When tracing through the code when the event is triggered, the debugger goes into…

Steven Jeuris
- 18,274
- 9
- 70
- 161
63
votes
4 answers
JavaScript anonymous function immediate invocation/execution (expression vs. declaration)
Possible Duplicates:
What is the difference between a function expression vs declaration in JavaScript?
Explain JavaScript's encapsulated anonymous function syntax
Why this:
(function () {
//code
}());
and this:
var f = function () {
…

lxa
- 3,234
- 2
- 30
- 31
62
votes
3 answers
(...()) vs. (...)() in javascript closures
I know this is silly, but there's any difference between this:
(function() {
var foo = 'bar';
})();
and this?
(function() {
var foo = 'bar';
}());
JSLint tells us to Move the invocation into the parens that contain the function,…

Camilo Martin
- 37,236
- 20
- 111
- 154
60
votes
4 answers
Anonymous functions using GCC statement expressions
This question isn't terribly specific; it's really for my own C enrichment and I hope others can find it useful as well.
Disclaimer: I know many will have the impulse to respond with "if you're trying to do FP then just use a functional language". I…

Bill
- 2,319
- 9
- 29
- 36