Skip to content

Instantly share code, notes, and snippets.

@yangsu
Created January 29, 2013 21:05
Show Gist options
  • Save yangsu/4667863 to your computer and use it in GitHub Desktop.
Save yangsu/4667863 to your computer and use it in GitHub Desktop.
JavaScript Funtions Example
foo(1, 2, 3); // Works fine because we declared function using
// the function literal at the bottom
bar(1, 2, 3); // => ReferenceError: bar is not defined
// The functions name is optional
// Here we are storing an anonymous function in a variable bar
var bar = function (a, b, c) {
// ...
};
// functions can be stored
var obj = {
baz: function () {
},
// you may use the optional name if recursion is needed
fib: function fibbinator (n) {
// ...
return fibbinator(n-1) + fibbinator(n-2);
}
};
function foo (a, b, c) {
// You can nest functions
function innerFoo() {
// ...
}
return innerFoo();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment