anonymous function javascript
// (param1, param2, paramN) => expression
// ES5
var multiplyES5 = function(x, y) {
return x * y;
};
// ES6
const multiplyES6 = (x, y) => { return x * y };
0
0
1 int function (int a) {
2 return a + 3;
3 }
4
5 class Functor {
6 public:
7 int operator()(int a) {
8 return a + 3;
9 }
10 };
11
12 int main() {
13 auto lambda = [] (int a) { return a + 3; };
14
15 Functor functor;
16
17 volatile int y1 = function(5);
18 volatile int y2 = functor(5);
19 volatile int y3 = lambda(5);
20
21 return 0;
22 }
Thank you!
0
0
4.25
8
// There are several definitions
// Non-anonymous, you name it
function hello() { /* code */ }
// Call as usual
hello()
// The myriad of anonymous functions
// This is actually anonymous
// It is simply stored in a variable
var hello = function() { /* code */ }
// It is called the same way
hello()
// You will usually find them as callbacks
setTimeout(function(){ /* code */ }, 1000)
// jQuery
$('.some-element').each(function(index){ /* code */ })
// Or a self firing-closue
(function(){ /* code */ })()
Thank you!
8
0
3
2
//Anonymous function; It's not assigned to an indentifier
Array.forEach((arguments) => {
//This is an anonymous function
})
function forEach(arguments) {
//This is NOT an anonymous function
}
Thank you!
2
0
3.88
8
// ES5
var multiplyES5 = function(x, y) {
return x * y;
};
// ES6
const multiplyES6 = (x, y) => { return x * y };
Thank you!
8
0
4.4
10
// Arrow functions
// ES6 introduced arrow function expression that provides a shorthand for declaring anonymous functions:
// For example, this function:
let show = function () {
console.log('Anonymous function');
};
// Code language: JavaScript (javascript)
// … can be shortened using the following arrow function:
let show = () => console.log('Anonymous function');
let show = (variable) => { /* code */ }
// Code language: JavaScript (javascript)
// Similarly, the following anonymous function:
let add = function (a, b) {
return a + b;
};
// Code language: JavaScript (javascript)
// … is equivalent to the following arrow function:
let add = (a, b) => a + b;
Thank you!
10
0
Are there any code examples left?
New code examples in category Javascript