Function
1. General function
Format:
function function name (formal parameters...)
{
Execution statement;
return return value;
}
A function is a package of multiple execution statements and will only be run when called.
Note: If you call a function with parameters but do not pass a value to it, the function will still run. Or if you call a function without parameters and pass a value to it, the function will still run.
To put it simply: as long as you write the function name followed by a pair of parentheses, the function will run. What about the parameters passed?
In fact, there is a parameter array object (arguments) in the function, which encapsulates the passed parameters in an array.
Example:
1 2 3 4 | function demo(){<span style= "font-family: 宋体;" >
alert(arguments.length);
}
demo(“hello”,123,true);
|
Copy after login
The result of the pop-up dialog box is 3. If you want to get all parameter values, you can traverse the array through a for loop.
1 2 3 | for ( var x=0; x<arguments.length; x++){
alert(arguments[x]);
}
|
Copy after login
In order to enhance readability, it is best to pass the actual parameters according to the defined formal parameters according to the specifications.
Other ways to write the function when calling:
1 2 3 | var show = demo();
var show = demo;
|
Copy after login
1 2 3 4 5 6 7 8 9 10 11 | <script type= "text/javascript" >
function getValue(){
<span style= "white-space:pre" > </span>alert( "aa" );
return 100;
<span style= "white-space:pre" > </span>}
var v2=getValue;
</script>
|
Copy after login
Although the function is declared as two parameters when it is defined, any number of
1 2 3 4 5 6 7 | <span style= "font-weight: normal;" ><span style= "font-size:12px;" > function show(x,y){
alert(x+ "," +y);
}
|
Copy after login
can be passed into each function when it is called. , there is a default array arguments, which stores all the actual parameters passed in during this call
1 2 3 4 5 6 7 8 9 10 11 12 13 | function show2(x,y){
arguments[0]=1000;
document.write(x+ "," +y+ "<br/>" );
for ( var i=0;i<arguments.length;i++){
<span style= "white-space:pre" > </span>document.write(arguments[i]+ "," );
<span style= "white-space:pre" > </span>}
}
show2(11,22,33,44);
|
Copy after login
Technical details:
1. There is no overloading of functions in js, and they are only identified by function names. The function name is the reference name of the function object
2. The parameters of the function are all received and stored internally by js using an array of arguments ------ This object is implicitly created for us by js internally. , and we can access and change the value
2. Dynamic function
is implemented through the built-in object Function of Js.
Example:
1 2 3 4 5 | <script type= "text/javascript" >
<span style= "white-space:pre" > </span> var add = new Function( "a,b" , "var s = a+b; return s; " );
</script>
|
Copy after login
Same as:
1 2 3 4 | function demo(x,y){
alert(x+y);
}
demo(4,6);
|
Copy after login
The difference is that for dynamic functions, parameters and function bodies can be passed through parameters and can be specified dynamically.
3. Anonymous function
Format: function(){...}
Example:
1 2 | var demo = function (){...}
demo();
|
Copy after login
Usually used when defining the behavior of event attributes Commonly used.
Example:
1 2 3 4 5 | function test()
{
alert(“load ok”);
}
window.onload = test;
|
Copy after login
can be written in the form of an anonymous function: 1 2 3 4 | window.onload = function ()
{
alert(“load ok”);
}
|
Copy after login
Anonymous function is an abbreviation format.
Examples of function definition and calling:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | <html>
<head>
<title>javascript数组与函数练习</title>
</head>
<body>
<script type= "text/javascript" >
function getMax(arr){
var max=0;
for ( var x=1;x<arr.length;x++){
if (arr[x]>arr[max]){
max = x;
}
}
return arr[max];
}
var arr=[23,-3,45,0,-100,47,22];
var v = getMax(arr);
function sortArray(arr){
for ( var x=0;x<arr.length-1;x++){
for ( var y=x+1;y<arr.length;y++){
if (arr[x]>arr[y]){
swap(arr,x,y);
}
}
}
}
function swap(arr,x,y){
var temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
function println(str){
document.write(str+ "<br/>" );
}
println(arr);
sortArray(arr);
println(arr);
function searchElement(arr,key){
for ( var x=0;x<arr.length;x++){
if (arr[x]==key){
return x;
}
}
return -1;
}
println( searchElement(arr,0) );
println( searchElement(arr,123) );
</script>
<script type= "text/javascript" >
function binarySearch(arr,key){
var max,min,mid;
min=0;
max=arr.length-1;
while (min<=max){
mid = (max+min)>>1;
if (key>arr[mid]){
min = mid+1;
} else if (key<arr[mid]){
max = mid-1;
} else {
return mid;
}
}
return -1;
}
println( binarySearch(arr,0) );
println( binarySearch(arr,123) );
function reverseArray(arr){
for ( var start=0, end =arr.length-1; start< end ; start++, end --){
swap(arr,start, end );
}
}
reverseArray(arr);
println( "反转之后:" +arr);
</script>
</body>
</html>
|
Copy after login
The above is the content of JavaScript web programming------functions (general functions, dynamic functions, anonymous functions). For more related information, please Follow the PHP Chinese website (www.php.cn)!