JavaScript------Functions (general functions, dynamic functions, anonymous functions)

Source: Internet
Author: User

javascript------Functions (general functions, dynamic functions, anonymous functions)
function

First, the general function

1. Format:

Function name (formal parameter ...)

{

Execute the statement;

return value;

}

A function is a wrapper for multiple execution statements and is only run if called.

Note: Calling a function that has parameters, but not giving it a value, can run as a function, or call a function that has no parameters, and give it a value, and the function runs as well.

The simple point: Just write the function name followed by a pair of parentheses, the function will run.

2, although the function is defined as two parameters, but the call can be passed to any of the

Cases:

?
123 <span style="font-size:14px;">function show(x,y){         alert(x+:+y);     }</span>
Show (4,8); Results: 4:8

Show (4); Result: 4:undefined

Show (); Results: undefined:undefined

Show (4,8,89); Results: 4:8

In a comprehensive example, the function in JS is not overloaded. If there is, it must be equipped with all of them.

3. In each function, there is a default array arguments, which stores all the arguments passed in at the time of the call

?
12 <span style="font-size:14px;">function show2(x,y){         for(var i=0; i</arguments.length;i++){></span>
Results: 1 2 3 45

4. What else does the function do when it is called?
123456789101112131415161718 <span style="font-size:14px;"><script type=text/javascript>      function getSum(){          return 100;      }      var sum = getSum();     // alert(sum); //结果:100         var sum2 = getSum; //相当于java当中的 引用捆绑      //alert(getSum); //结果:getSum.toString()      // alert(sum2); //结果:sum2.toString(),也就是getSum.toString()      //alert(sum2()); //等价于调用:getSum()            function show2(){          alert(kkkl);      }      alert( show2() ); //先弹出“kkkl”,再弹出“undefined”。因为里面的函数没有return值,而外面的函数要用变量去接,结果相当于变量没赋值就输出         </script></span>


Second, dynamic function

Using the built-in object function in JS to construct a function, the 1th parameter in the construction method is "formal parameter", and the 2nd parameter is "function body".

The idea is similar to class reflection in Java. We usually don't use the function when we write it, but the key is that the function of the whole program becomes very alive.

?
123 <span style="font-size:14px;">var add = newFunction(x,y, var sum; sum=x+y;returnsum;);var s = add(100,39);alert(s=+s);</span>

Third, anonymous function

Format: function () {...}

Cases:

?
12 var demo = function(){...}demo();

It is often more common to define the behavior of an event property.

Cases:

?
12345 function test(){alert(“load ok”);}window.onload = test;

Can be written in the form of an anonymous function:

?
1234 window.onload = function(){alert(“load ok”);}

An anonymous function is a shorthand format.


Four, function call

Function.js

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071       //1 取最大值:输出给定数组当中值最大的元素      //函数定义      function getMax(arr){          var max=0;//记下标          for(var x=1; xarr[max]){                  max = x;              }          }          return arr[max];      }                //2 数组排序      //函数定义      function sortArray(arr){          for(var x=0; xarr[y]){                      swap(arr,x,y);                  }              }          }      }      //辅助函数,用于交换数组中的两个元素      function swap(arr,x,y){          var temp = arr[x];          arr[x] = arr[y];          arr[y] = temp;      }                 //3 模拟Java当中的System.out.println()      function println(str){          document.write( str +);      }                  //4 在数组当中查找元素(若存在则返回该元素的位置,否则返回-1)      function searchElement(arr,key){          for(var x=0; x>1;              if(key>arr[mid]){                  min=mid+1;              }else if(key调用:<pre class="brush:java;"></pre><script type=text/javascript src=functions.js>    </script><script type=text/javascript>       //1 函数调用      var arr=[133,-22,33,43,1211];      var mValue = getMax(arr);      //alert(mValue=+ mValue);          //2函数调用      document.write(排序前:+arr +<br>); // println(arr);      sortArray(arr);      document.write(排序后:+arr);            //3 函数调用      println();      println(arr);            //4 函数调用      var a=searchElement(arr,133);      //alert(a);            //5 函数调用      var b=binarySearch(arr,-2);      //alert(b);            //6 函数调用      reverseArray(arr);      println(arr);         </script></arr[mid]){></arr.length;></arr.length-1;></arr.length;>

 

JavaScript------Functions (general functions, dynamic functions, anonymous functions)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.