I. Definition
[Javascript]
Function print (msg)
{
Document. write (msg, "<br> ");
}
Function distance (x1, y1, x2, y2)
{
Var dx= x2-x1;
Var dy = y2-y1;
Return Math. sgrt (dx * dx + dy * dy );
}
Function factorial (x)
{
If (x <= 1)
Return 1;
Return x * factorial (x-1 );
}
// Call
Print ("Hello" + name );
Print ("Welcome to my blog ");
Total_dist = distance (,) + distance );
Print ("The probability of that is:" + factorial (5)/factorial (13 ));
// Nested Functions
Function hypotenuse (a, B)
{
Function square (x)
{
Return x * x;
}
Return Math. sqrt (square (a) + square (B ));
}
2. Variable Length Parameter List: Arguments object
[Javascript]
// Use arguments to verify whether the correct number of parameters are used when the function is called
Function f (x, y, z)
{
If (arguments. length! = 3)
{
Throw new Error ("functon f called with" + arguments. length + "arguments, but it expects 3 arguments .");
}
}
// Arguments can be used to write functions so that they can use any number of actual parameters.
Function max (/*...*/)
{
Var m = Number, NEGATIVE_INFINITY;
For (var I = 0; I <arguments. length; I ++)
{
If (arguments [I]> m) m = arguments [I];
Return m;
}
}
Var largest = max );
// Such a parameter that can accept any number is called a Variable Parameter Function.
// The callee attribute, used to reference the currently executed function. It can be used to allow recursive calls to the unnamed function itself.
Function (x)
{
If (x <= 1) return 1;
Return x * arguments. callee (x-1 );
}
3. Use object attributes as parameters
To facilitate the call, you can use the following method to allow parameters to be passed by name/value pair in any order.
[Javascript]
Function arraycopy (/* array */from,/* index */from_start,/* array */to,/* index */to_strart,/* integer */length)
{
// Code goes here
}
Function easycopy (args)
{
Arraycopy (
Args. from,
Args. from_start | 0,
Args.,
Args. to_start | 0,
Args. length ;);
}
Var a = [1, 2, 4];
Var B = new Array (4 );
Easycopy (from: a, to: B, length: 4 );
Iv. parameter types
[Javascript]
// Verify the function of the parameter type. If the function fails, the execution is interrupted.
Function sum ()
{
If (a instanseof Array |
(A & typeof a = "object" & "length" in ))
{
Var total = 0;
For (var I = 0; I <a. length; I ++)
{
Var element = a [I];
If (! Element) continue;
If (typeof element = "number") total + = element;
Else throw new Error ("sum (): all array elements must be numbers ");
}
Return total;
}
Else throw new Error ("sum (): argument must be an array ");
}
// The following function can accept any number of parameters and recursively process all parameters of the array type. before throwing an error, do your best to convert non-numeric values to numbers.
Function flexisum ()
{
Var total = 0;
For (var I = 0; I <arguments. length; I ++)
{
Var element = arguments [I];
If (! Element) continue;
Var n;
Swith (typeof element)
{
Case "number ":
N = element;
Break;
Case "object ":
If (element instanceof Array)
N = flexisum. apply (this, element );
Else n = element. valueof ();
Break;
Case "function ":
N = element ();
Break;
Case "string ":
N = parseFloat (element );
Break;
Case "boolean ":
N = NaN;
Break;
}
Else throw new Error ("sum (): can't convert" + element + "to number ");
} Www.2cto.com
Return total;
}
By: dxh_0829