@@ -8,81 +8,88 @@ can take zero or more arguments, and can optionally return a value.
8
8
9
9
Functions can be created in a variety of ways:
10
10
11
- <javascript caption =" Function Declaration " >
12
- function foo() { /* do something */ }
13
- </javascript >
11
+ ``` js
12
+ // Function Declaration
13
+ function foo () { /* do something */ }
14
+ ```
14
15
15
- <javascript caption =" Named Function Expression " >
16
- var foo = function() { /* do something */ }
17
- </javascript >
16
+ ``` js
17
+ // Named Function Expression
18
+ var foo = function () { /* do something */ }
19
+ ```
18
20
19
21
I prefer the named function expression method of setting a function's name, for
20
22
some rather in-depth and technical reasons. You are likely to see both methods
21
23
used in others' JavaScript code.
22
24
23
25
## Using Functions
24
26
25
- <javascript caption =" A simple function " >
27
+ ``` js
28
+ // A simple function
26
29
var greet = function (person , greeting ) {
27
30
var text = greeting + ' , ' + person;
28
31
console .log (text);
29
32
};
30
33
31
34
greet (' Rebecca' , ' Hello' );
32
- </ javascript >
35
+ ```
33
36
34
-
35
- < javascript caption = " A function that returns a value " >
37
+ ``` js
38
+ // A function that returns a value
36
39
var greet = function (person , greeting ) {
37
40
var text = greeting + ' , ' + person;
38
41
return text;
39
42
};
40
43
41
44
console .log (greet (' Rebecca' ,' hello' ));
42
- </ javascript >
45
+ ```
43
46
44
- <javascript caption =" A function that returns another function " >
47
+ ``` js
48
+ // A function that returns another function
45
49
var greet = function (person , greeting ) {
46
50
var text = greeting + ' , ' + person;
47
51
return function () { console .log (text); };
48
52
};
49
53
50
54
var greeting = greet (' Rebecca' , ' Hello' );
51
55
greeting ();
52
- </ javascript >
56
+ ```
53
57
54
58
## Immediately-Invoked Function Expression
55
59
56
60
A common pattern in JavaScript is the immediately-invoked function expression. This
57
61
pattern creates a function expression and then immediately executes the
58
62
function. This pattern is extremely useful for cases where you want to avoid
59
- polluting the global namespace with your code — no variables declared inside of
63
+ polluting the global namespace with your code &# 8212 ; no variables declared inside of
60
64
the function are visible outside of it.
61
65
62
- <javascript caption =" An immediately-invoked function expression " >
66
+ ``` js
67
+ // An immediately-invoked function expression
63
68
(function (){
64
69
var foo = ' Hello world' ;
65
70
})();
66
71
67
72
console .log (foo); // undefined!
68
- </ javascript >
73
+ ```
69
74
70
75
## Functions as Arguments
71
76
72
77
In JavaScript, functions are "first-class citizens" - they can be assigned
73
78
to variables or passed to other functions as arguments. Passing functions as
74
79
arguments is an extremely common idiom in jQuery.
75
80
76
- <javascript caption =" Passing an anonymous function as an argument " >
81
+ ``` js
82
+ // Passing an anonymous function as an argument
77
83
var myFn = function (fn ) {
78
84
var result = fn ();
79
85
console .log (result);
80
86
};
81
87
82
88
myFn (function () { return ' hello world' ; }); // logs 'hello world'
83
- </ javascript >
89
+ ```
84
90
85
- <javascript caption =" Passing a named function as an argument " >
91
+ ``` js
92
+ // Passing a named function as an argument
86
93
87
94
var myFn = function (fn ) {
88
95
var result = fn ();
@@ -94,4 +101,4 @@ myFn(function() { return 'hello world'; }); // logs 'hello world'
94
101
};
95
102
96
103
myFn (myOtherFn); // logs 'hello world'
97
- </ javascript >
104
+ ```
0 commit comments