-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_class_function.js
More file actions
42 lines (29 loc) · 917 Bytes
/
Copy pathfirst_class_function.js
File metadata and controls
42 lines (29 loc) · 917 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// #1. Passing Functions as Arguments:
function greet(name) {
console.log('Hello, ' + name + '!');
}
function executeCallback(callback) {
callback('John');
}
executeCallback(greet); // Pass the greet function as an argument
// #2 Returning Functions from Functions:
function createGreeter() {
function greet(name) {
console.log('Hello, ' + name + '!');
}
return greet; // Return the greet function
}
const greeter = createGreeter();
greeter('Jane'); // Call the returned function
// 3. Assigning Functions to Variables:
const sayHello = function (name) {
console.log('Hello, ' + name + '!');
};
sayHello('Alice'); // Call the function using the variable name
// 4. Storing Functions in Data Structures:
const functions = {
greet: function (name) {
console.log('Hello, ' + name + '!');
}
};
functions.greet('Bob'); // Call the function stored in the object