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

Source: Internet
Author: User

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

Function

 

I. general functions

 

1. format:

Function Name (formal parameter ...)

{

Execute the statement;

Return value;

}

A function is the encapsulation body of multiple execution statements. It is run only when called.

Note: If you call a function with parameters but do not pass the value to it, the function can run the same way, or call a function without parameters and pass the value to the function, and the function runs the same way.

To put it simply, you only need to write a function name followed by a pair of parentheses, and the function will run.

 

2. Although the function is defined as two parameters, it can be passed into any

Example:

function show(x,y){     alert(x+:+y);     }
Result: 4: 8

 

Show (4); Result: 4: undefined

Show (); Result: undefined

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

In summary, functions in Javascript do not have heavy loads. If yes, all of them will be available.

 

3. Each function has a default array of arguments, which stores all the arguments passed in during this call.

 

function show2(x,y){     for(var i=0; i
Result: 1 2 3 45

 

 

4. Other functions used for calling
<Script type = text/javascript> function getSum () {return 100;} var sum = getSum (); // alert (sum); // result: 100 var sum2 = getSum; // equivalent to the reference bundle in java // alert (getSum); // result: getSum. toString () // alert (sum2); // result: sum2.toString (), that is, getSum. toString () // alert (sum2 (); // equivalent to calling: getSum () function show2 () {alert (kkkl);} alert (show2 ()); // "kkkl" is displayed, and "undefined" is displayed ". Because the function does not have a return value, and the external function must be connected with the variable, the result is equivalent to output if the variable is not assigned a value </script>


 

Ii. Dynamic Functions

 

Construct a Function using the built-in object Function in Js. The 1st parameters in the constructor are "form parameters", and the 2nd parameters are "Function bodies ".

This idea is similar to class reflection in Java. We usually do not need to write functions at ordinary times, But When writing the key point, the function of the entire program will become very active.

 

var add = new Function(x,y, var sum; sum=x+y;return sum;);var s = add(100,39);alert(s=+s);

 

 

Iii. Anonymous Functions

 

Format: function (){...}

Example:

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

 

It is usually used to define the behavior of event attributes.

Example:

 

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

 

It can be written as an anonymous function:

 

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

 

An anonymous function is a short format.


Iv. function call

Function. js

 

// 1 get the maximum value: output the element with the maximum value of the given array // function definition function getMax (arr) {var max = 0; // record the subscript for (var x = 1; xarr [max]) {max = x ;}} return arr [max] ;}// 2 array sorting // function definition function sortArray (arr) {for (var x = 0; xarr [y]) {swap (arr, x, y) ;}}// helper function, function swap (arr, x, y) {var temp = arr [x]; arr [x] = arr [y]; arr [y] = temp;} // 3 simulate the System in Java. out. println () function println (str) {document. write (str +);} // 4 search for elements in the array (if any, return the position of the element; otherwise, return-1) function searchElement (arr, key) {for (var x = 0; x> 1; if (key> arr [mid]) {min = mid + 1;} else if (key call:
<Script type = text/javascript src = functions. js> </script> <script type = text/javascript> // 1 function call var arr = [133,-,]; var mValue = getMax (arr ); // alert (mValue = + mValue); // 2 function calls document. write (Before sorting: + arr +
); // Println (arr); sortArray (arr); document. write (after sorting: + arr); // 3 function call println (); println (arr); // 4 function call var a = searchElement (arr, 133 ); // alert (a); // 5 function call var B = binarySearch (arr,-2); // alert (B); // 6 function call reverseArray (arr ); println (arr); </script>



 

 

 

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.