Example 1 and 2 are completely different. jQuery IS javascript, so function definitions are the same.
$(function(){... is just shorthand for $(document).ready(function(){... while example one is actually producing a new function called add.
You could produce the add function within the ready function like this:
$(function(){
function add(){
var foo = 1;
}
});
jQuery is not a seporate new language with different definitions and syntax than javascript, its a toolkit written in javascript using javascript's syntax and definitions.
Think of jQuery itself as just a big function, a function defined as $ ... so instead of function add(){} jquery is just a function called $ function $(){}. You also pass jQuery arguments with the same syntax as a normal javascript function.
function add(arg){
//do something with arg
}
add('#elem');
function $(arg){
//do something with arg
}
$('#elem');
You see? jQuery is just a big complicated function that we can pass many types of arguments, and returns many useful tools. But the syntax is and definitions are no different from traditional javascript.
function add(arg){
var added = arg + 12;
return this.alertAdded = function(){
alert(added);
}
}
add(30).alertAdded(); // will alert 42
//vs
$('#elem').fadeOut(); // same syntax but referring to very different functionality.
here is an example of jQueryish syntax is normal plain old JS.