Skip to content

Commit be32bca

Browse files
committed
Converted 'functions' to the new code convention.
1 parent d37b781 commit be32bca

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

page/javascript-101/functions.md

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,81 +8,88 @@ can take zero or more arguments, and can optionally return a value.
88

99
Functions can be created in a variety of ways:
1010

11-
<javascript caption="Function Declaration">
12-
function foo() { /* do something */ }
13-
</javascript>
11+
``` js
12+
// Function Declaration
13+
function foo() { /* do something */ }
14+
```
1415

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+
```
1820

1921
I prefer the named function expression method of setting a function's name, for
2022
some rather in-depth and technical reasons. You are likely to see both methods
2123
used in others' JavaScript code.
2224

2325
## Using Functions
2426

25-
<javascript caption="A simple function">
27+
``` js
28+
// A simple function
2629
var greet = function(person, greeting) {
2730
var text = greeting + ', ' + person;
2831
console.log(text);
2932
};
3033

3134
greet('Rebecca', 'Hello');
32-
</javascript>
35+
```
3336

34-
35-
<javascript caption="A function that returns a value">
37+
``` js
38+
// A function that returns a value
3639
var greet = function(person, greeting) {
3740
var text = greeting + ', ' + person;
3841
return text;
3942
};
4043

4144
console.log(greet('Rebecca','hello'));
42-
</javascript>
45+
```
4346

44-
<javascript caption="A function that returns another function">
47+
``` js
48+
// A function that returns another function
4549
var greet = function(person, greeting) {
4650
var text = greeting + ', ' + person;
4751
return function() { console.log(text); };
4852
};
4953

5054
var greeting = greet('Rebecca', 'Hello');
5155
greeting();
52-
</javascript>
56+
```
5357

5458
## Immediately-Invoked Function Expression
5559

5660
A common pattern in JavaScript is the immediately-invoked function expression. This
5761
pattern creates a function expression and then immediately executes the
5862
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
6064
the function are visible outside of it.
6165

62-
<javascript caption="An immediately-invoked function expression">
66+
``` js
67+
// An immediately-invoked function expression
6368
(function(){
6469
var foo = 'Hello world';
6570
})();
6671

6772
console.log(foo); // undefined!
68-
</javascript>
73+
```
6974

7075
## Functions as Arguments
7176

7277
In JavaScript, functions are "first-class citizens" - they can be assigned
7378
to variables or passed to other functions as arguments. Passing functions as
7479
arguments is an extremely common idiom in jQuery.
7580

76-
<javascript caption="Passing an anonymous function as an argument">
81+
``` js
82+
// Passing an anonymous function as an argument
7783
var myFn = function(fn) {
7884
var result = fn();
7985
console.log(result);
8086
};
8187

8288
myFn(function() { return 'hello world'; }); // logs 'hello world'
83-
</javascript>
89+
```
8490

85-
<javascript caption="Passing a named function as an argument">
91+
``` js
92+
// Passing a named function as an argument
8693

8794
var myFn = function(fn) {
8895
var result = fn();
@@ -94,4 +101,4 @@ myFn(function() { return 'hello world'; }); // logs 'hello world'
94101
};
95102

96103
myFn(myOtherFn); // logs 'hello world'
97-
</javascript>
104+
```

0 commit comments

Comments
 (0)