Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Converted 'functions' to the new code convention.
  • Loading branch information
RedWolves committed Oct 15, 2012
commit be32bca24468f9887ee5788618973cf8575fe980
47 changes: 27 additions & 20 deletions page/javascript-101/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,81 +8,88 @@ can take zero or more arguments, and can optionally return a value.

Functions can be created in a variety of ways:

<javascript caption="Function Declaration">
function foo() { /* do something */ }
</javascript>
``` js
// Function Declaration
function foo() { /* do something */ }
```

<javascript caption="Named Function Expression">
var foo = function() { /* do something */ }
</javascript>
``` js
// Named Function Expression
var foo = function() { /* do something */ }
```

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

## Using Functions

<javascript caption="A simple function">
``` js
// A simple function
var greet = function(person, greeting) {
var text = greeting + ', ' + person;
console.log(text);
};

greet('Rebecca', 'Hello');
</javascript>
```


<javascript caption="A function that returns a value">
``` js
// A function that returns a value
var greet = function(person, greeting) {
var text = greeting + ', ' + person;
return text;
};

console.log(greet('Rebecca','hello'));
</javascript>
```

<javascript caption="A function that returns another function">
``` js
// A function that returns another function
var greet = function(person, greeting) {
var text = greeting + ', ' + person;
return function() { console.log(text); };
};

var greeting = greet('Rebecca', 'Hello');
greeting();
</javascript>
```

## Immediately-Invoked Function Expression

A common pattern in JavaScript is the immediately-invoked function expression. This
pattern creates a function expression and then immediately executes the
function. This pattern is extremely useful for cases where you want to avoid
polluting the global namespace with your code no variables declared inside of
polluting the global namespace with your code &#8212; no variables declared inside of
the function are visible outside of it.

<javascript caption="An immediately-invoked function expression">
``` js
// An immediately-invoked function expression
(function(){
var foo = 'Hello world';
})();

console.log(foo); // undefined!
</javascript>
```

## Functions as Arguments

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

<javascript caption="Passing an anonymous function as an argument">
``` js
// Passing an anonymous function as an argument
var myFn = function(fn) {
var result = fn();
console.log(result);
};

myFn(function() { return 'hello world'; }); // logs 'hello world'
</javascript>
```

<javascript caption="Passing a named function as an argument">
``` js
// Passing a named function as an argument

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

myFn(myOtherFn); // logs 'hello world'
</javascript>
```