Functions which do not have names are called "anonymous functions".
THIS IS A STANDARD FUNCTION
function double(x){return x * 2;}
document.write( double(5) );
THIS IS AN ANONYMOUS FUNCTION
document.write( function(x){return x * 2;} (5) );
Both of the above functions do the same thing.
you can assign anonymous functions to variables
var myfunc = function(x){return x * 2;}
document.write( myfunc(5) );
Dont Forget the Pre-defined function
var r = "";
var s = "A long and winding road";
document.write("Original = " + s + "
");
r = s.replace("long", "short"); // <== PRE DEFINED FUNCTION
document.write("Result = " + r);
Functions are Objects
The Frog object
function FrogObject(objName)
{
this.name = objName;
this.sayHi = function () { alert('Hi my name is '+this.name); };
this.sayBye = new Function ("alert('Bye!'); ");
this.sayAnything = function (msg) { alert(this.name+' says '+msg); };
}
var frog = new FrogObject("Kermit");
frog.sayHi();
frog.sayAnything("rivitttt...");
frog.sayBye();
Using "this" is a quick way of referencing the current object.
In JavaScript every function must return a value.
The default value is undefined, except for constructors,
where the default return value is "this".