Everest -4. Function function

Function function

The method is a function or a function thereof is the function code for the function in a packaged together for later wants to operate to achieve this function, just need to perform the function => 'package': reduce the redundancy page code to improve code reuse (low coupling high cohesion)

  • Washing machine is a function that generates a washing machine is packaged function (the code that implements certain features of the package comes in), the production time ,, do not know what to put user when the water washing clothes, clothes, laundry detergent, we need to provide access (provided after the processing Anhui farmers: an inlet parameter in the function is called, executed when a function called something specific arguments put in), required washing clothes could get out, a washing machine outlet (called the return value of the function the result can be used to return to the outside)
  • Creating function
    • Katachisan
    • return value
  • Perform functions
    • Arguments
  • arguments
    • A function of the underlying operating mechanism

Creating function

ES5 老方式:
function [函数名](形参变量1,...){
  函数体:基于js完成需要实现的功能
  return [处理后的结果];
}
[函数名](实参1,...);

    //求两个数的和,算完和后乘以10,然后再除以2.。。
    // sum 是函数名,代表这个函数本身,
    // sun()是让函数执行,代表的是函数执行返回的结果
    // n/m 是形参,是变量 ,用来储存执行函数式传递的实参
    
    function sum(n, m) { // n m 形参 进水口
        let res = n + m;
        res *= 10;
        res /= 2;
        console.log(res);
    }
    sum(4, 6)// 4 6 实参   

return

    //==============函数中的返回值
    // 函数执行的时候,函数体内部创建的变量我们是无法获取和操作的
    /*       function sum(n, m) {
                let res = n + m;
                //return 的一定是值:此处是吧res变量储存的值返回给外面;
                return res;
            }
            let A = sum(10, 250)
            console.log(A); */

    /*      function sum(n, m) {
                if (n === undefined || m === undefined) {
                    // 函数体中遇到return,后面代码则不再执行了
                    return;
                }
            } */

Anonymous function

    //  ==============匿名函数
    // 匿名函数之函数表达式:把一个匿名函数本身作为值复制给其它东西,这种函数一般不是手动触发执行,,而且靠其它程序去东莞触发执行
    // document.body.onclick = function () { };

    // ===========自执行函数:创建完一个匿名函数,紧接着就把当前函数家小括号执行
    (function (n) { })(1000);

Guess you like

Origin www.cnblogs.com/divtab/p/11655878.html