Questions tagged [anonymous-function]

Anonymous functions use a block of code as a value, defining it as an inline function without a name.

2121 questions
402
votes
10 answers

Explain the encapsulated anonymous function syntax

Summary Can you explain the reasoning behind the syntax for encapsulated anonymous functions in JavaScript? Why does this work: (function(){})(); but this doesn't: function(){}();? What I know In JavaScript, one creates a named function like…
Prem
  • 15,911
  • 11
  • 31
  • 35
391
votes
19 answers

Why do you need to invoke an anonymous function on the same line?

I was reading some posts about closures and saw this everywhere, but there is no clear explanation how it works - everytime I was just told to use it...: // Create a new anonymous function, to use as a wrapper (function(){ // The variable that…
palig
  • 7,651
  • 6
  • 24
  • 18
332
votes
5 answers

How to sort with lambda in Python

I am trying to sort some values by attribute, like so: a = sorted(a, lambda x: x.modified, reverse=True) I get this error message: () takes exactly 1 argument (2 given) Why? How do I fix it? This question was originally written for Python…
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
238
votes
6 answers

Anonymous recursive PHP functions

Is it possible to have a PHP function that is both recursive and anonymous? This is my attempt to get it to work, but it doesn't pass in the function name. $factorial = function( $n ) use ( $factorial ) { if( $n <= 1 ) return 1; return…
Kendall Hopkins
  • 43,213
  • 17
  • 66
  • 89
152
votes
18 answers

removeEventListener on anonymous functions in JavaScript

I have an object that has methods in it. These methods are put into the object inside an anonymous function. It looks like this: var t = {}; window.document.addEventListener("keydown", function(e) { t.scroll = function(x, y) { …
bitkid
  • 1,631
  • 2
  • 11
  • 4
141
votes
21 answers

javascript: recursive anonymous function?

Let's say I have a basic recursive function: function recur(data) { data = data+1; var nothing = function() { recur(data); } nothing(); } How could I do this if I have an anonymous function such as... (function(data){ …
Incognito
  • 20,537
  • 15
  • 80
  • 120
122
votes
1 answer

Why do arrow functions not have the arguments array?

function foo(x) { console.log(arguments) } //foo(1) prints [1] but var bar = x => console.log(arguments) gives the following error when invoked in the same way: Uncaught ReferenceError: arguments is not defined
Conqueror
  • 4,265
  • 7
  • 35
  • 41
113
votes
4 answers

Location of parenthesis for auto-executing anonymous JavaScript functions?

I was recently comparing the current version of json2.js with the version I had in my project and noticed a difference in how the function expression was created and self executed. The code used to wrap an anonymous function in parenthesis and then…
Kevin Hakanson
  • 41,386
  • 23
  • 126
  • 155
103
votes
11 answers

Is it valid to define functions in JSON results?

Part of a website's JSON response had this (... added for context): {..., now:function(){return(new Date).getTime()}, ...} Is adding anonymous functions to JSON valid? I would expect each time you access 'time' to return a different value.
Zachary Scott
  • 20,968
  • 35
  • 123
  • 205
102
votes
5 answers

Why use named function expressions?

We have two different way for doing function expression in JavaScript: Named function expression (NFE): var boo = function boo () { alert(1); }; Anonymous function expression: var boo = function () { alert(1); }; And both of them can be…
Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201
101
votes
6 answers

PHP 7.2 Function create_function() is deprecated

I have used create_function() in my application below. $callbacks[$delimiter] = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);"); But for PHP 7.2.0, create_function() is deprecated. How do I rewrite my code above for…
Saly
  • 1,446
  • 3
  • 11
  • 18
101
votes
4 answers

Lambda in a loop

Considering the following code snippet: # directorys == {'login': , 'home': } for d in directorys: self.command["cd " + d] = (lambda : self.root.change_directory(d)) I expect to create a dictionary of two function…
FunkySayu
  • 7,641
  • 10
  • 38
  • 61
98
votes
4 answers

Why is "this" in an anonymous function undefined when using strict?

Why is this in an anonymous function undefined when using javascript in strict mode? I understand why this could make sense, but I couldn't find any concrete answer. Example: (function () { "use strict"; this.foo = "bar"; // *this* is…
T. Junghans
  • 11,385
  • 7
  • 52
  • 75
96
votes
5 answers

php is_function() to determine if a variable is a function

I was pretty excited to read about anonymous functions in php, which let you declare a variable that is function easier than you could do with create_function. Now I am wondering if I have a function that is passed a variable, how can I check it to…
Jage
  • 7,990
  • 3
  • 32
  • 31
96
votes
6 answers

How can I write an anonymous function in Java?

Is it even possible?
aarona
  • 35,986
  • 41
  • 138
  • 186
1
2 3
99 100