High cohesion and low coupling

A function is another type of variable

Function basic application

example

function test(){ document.write("a"); document.write("b"); document.write("c"); } test(); test(); Write as much as you call itCopy the code

example

function test() { var a = 123; var b = 234; var c = a + b; document.write(c); } // execute test() after text();Copy the code

Two ways to define a function

A function declaration example
function theFirstName() { } document.write(theFirstName); // Refer to the function body, interpreted languages do not output the address,Copy the code
Two function expression
Named function expression

Test.name =” ABC”

    var test = function abc(){
         document.write("a");
     }
Copy the code
Anonymous function expressions are commonly used (simplified to function expressions)

Example function name demo.name=”demo”

    var demo = function(){
        document.write("b");
    }
Copy the code

Function composition (function name + argument + return value)

Function test(a,b){function test(a,b){function test() document.write(a+b); } the test (1, 2);Copy the code

Patients with a

function sum(a,b) { var c = a + b; document.write(c); }// sum(13,2); // Actual parametersCopy the code

Example 2

function sum(a,b) { if(a>10) { document.write(a - b); }else if(a<10) { document.write(a + b); }else{ document.write(10); }} sum (11, 5);Copy the code

The number of parameters and arguments may not be uniform

example

function sum(a,b,c,d) { document.write(a); document.write(d); } the sum (12,3,5);Copy the code

Example arguments — argument list [11,2,3]; arguments store the arguments as an array whether or not the parameter has received them

function sum(a,b,c) { console.log(sum.length); // The length of the parameter console.log(arguments); console.log(arguments.length); For (var I = 0; var I = 0; i < arguments.length; i++){ console.log(arguments[i]); }} sum(11,2,3,2,9);Copy the code

example

Function sum(a,b,c,d,e){console.log(" arguments ")}else if(sum.length<arguments.length){function sum(a,b,c,d,e){console.log(" arguments ")}else if(sum.length<arguments.length){ The console. The log (" argument more ")} else {the console. The log (" equal ")}} sum,2,3,2 (11);Copy the code

Summation function/addition counter

Function sum() {// arguments[1,2,3,4,5,6,7]; Var result = 0; For (var I = 0; i <arguments.length; i++){ result += arguments[i]; } console.log(result); } the sum (1,2,3,4,5,6,7,8,9,)Copy the code

/ / 1

Function sum(a,b) {//arguments [1,2] //var a = 1; a = 2; console.log(arguments[0]); } sum(1,2); Results: the arguments [0] = 2Copy the code

example

Function sum(arguments[0]) {console.log(arguments[0]); //arguments[0]=9 // Arguments [0]= 77; console.log(a); / / a = 77} sum (2 and 6)Copy the code

example

function sum(a,b) { b = 2; console.log(arguments[1]); } sum(1);} sum(1);} sum(1);Copy the code
return
One, abort function
function sum() { console.log("a"); return; console.log("b"); } the sum (1, 3);Copy the code
The value is returned outside the function and terminates the function
function sum(){ // console.log("a"); return 123; // console.log("a"); } var num = sum(); Num = 123 / / resultsCopy the code
Function myNumber(target) {//target converts the target to a number and returns return +target; Var num = myNumber("123"); console.log(typeof(num) + ":" +num);Copy the code

exercises

1 Write a function that tells you the call of the selected animal

function scream(animal) {
    switch(animal) {
        case "dog":
            document.write("wang!");
            return;
        case "cat":
            document.write("miao");
            return;
        case "fish":
            document.write("o~o~o~");
            return;
    }
}
Copy the code

Write a function to implement the add counter

function sum() { var result = 0; for(var i = 0; i<arguments.length; i++){ result += arguments[i]; } document.write(result); } sum(); // Type the number you want to add insideCopy the code

Define a set of functions, input numbers, reverse and output Chinese characters

Function reverse(){var num = window.prompt("input"); Var I = num. Length - 1; var I = num. i >= 0; i--) { str += transfer(num[i]); } document.write(str); } function transfer(target) {switch(target) {case "1" : return "one "; Elseif "2" : return "2"; Elseif "3" : return "3"; }}Copy the code

4 Write a function to implement n factorial ——— method n! = n *(n – 1)! ;

function jc(n) {
    if(n==1) {
        return 1;
    }
    return n * jc(n-1);
}
Copy the code

var n = parseInt(window.prompt(“input”));

Inputables represent variables, and parameters are variables, so they can be converted

// method multiplicative

function mul(n) { // var num = 1; // for(var i = 1; i <= n; i++); // num*=i;Copy the code

Return (1) {return (1); Find a pattern n! = n *(n – 1)! 2. Use what you know to find an exit and break out of the loop) to make the code cleaner

if(n == 1 || n == 0) { return 1; } return n * mul(n - 1); } mul(5); // Console printsCopy the code

5. Write a function that implements the Fibonacci sequence

Fn (n) = Fb (n-1) + Fb (n-2); function fb(n) { if(n == 1 || n == 2) { return 1; } return fb(n-1) +fb(n-2); } fb(3);Copy the code

The operation process

    fb(5) ==> fb(4) +fb(3);
    fb(4) ==> fb(3) +fb(2);
    fb(3) ==> fb(2) +fb(1) = 1 + 1;
Copy the code

Reference value

Var arr = [5,2,3,4,5,9,11]; console.log(arr[0]); // Print arr[0] = 3; // You can change it directlyCopy the code
Var arr =,2,3,4,5,9,11 [1]; for(var i = 0; i < arr.length; i++) { // console.log(arr[i]); // arr[I] += 1; // add 1}Copy the code

Two object object

Var deng = {lastName: "deng ", age: 40, sex: undefined, wife :"xiaoliu", father :"daye", son:"xiaodeng", handsome:false } console.log(deng.father); deng.father = "daniang"; console.log(deng.father);Copy the code