美文网首页
javascript入门4(0801)

javascript入门4(0801)

作者: LAYoung_f1b8 | 来源:发表于2019-08-02 09:20 被阅读0次

    1.函数的参数的个数问题

            <script>

                /*

                    函数参数的个数问题:

                        1、当给出形参,但却没有传入实参,则值为undefined

                        2、当形参的个数大于实参的个数

                        3、当形参的个数等于实参的个数

                        4、当形参的个数小于实参的个数

                        5、给出实参,但未设置形参

                 */

                // 1、当给出形参,但却没有传入实参,则值为undefined

                function fun1(a,b){

                    console.log(a)//undefined

                    console.log(b)//undefined

                }

                fun1();

                // 2、当形参的个数大于实参的个数

                function fun2(a,b){

                    console.log(a)//10

                    console.log(b)//undefined

                }

                fun2(10);

                // 3、当形参的个数等于实参的个数

                function fun3(a,b){

                    console.log(a)//10

                    console.log(b)//20

                }

                fun3(10,20);

                // 4、当形参的个数小于实参的个数

                function fun4(a){

                    console.log(a)//10

                }

                fun4(10,20);

                // 5、给出实参,但未设置形参

                function fun5(){

                    // 传入的实参可以用arguments接受,返回一个数组

                    console.log(arguments);

                }

                fun5(10,20)

            </script>

    2.返回值的情况

            <script>

                /*

                    函数的返回值

                        1、函数调用后未给出return,值为undefined

                        2、函数调用后给出return,但没有具体的值

                        3、函数运行后给出return,并给出具体的值

                        4、函数的返回值后面的代码不执行

                        5、函数的返回值可以返回对象

                 */

                // 1、函数调用后未给出return

                function fun1(){

                    console.log('今天建军节');

                }

                var res = fun1();

                console.log(res);//undefined

                // 2、函数调用后给出return,但没有具体的值

                function fun2(){

                    console.log('今天建军节');

                    return

                }

                var res = fun2();

                console.log(res);//undefined

                // 3、函数运行后给出return,并给出具体的值

                function plus(a,b){

                    return a+b;

                }

                var res = plus(4,9);

                console.log(res)

                // 4、函数的返回值后面的代码不执行

                function plus(a,b){

                    console.log('快来撩我');

                    return a+b;

                    console.log('捉迷藏');

                }

                var res = plus(4,9);

                console.log(res)

                // 5、函数的返回值可以返回对象

                function fun5(){

                    var name = '向同学';

                    // var obj = {

                    //  name:'翁宇',

                    //  age:18

                    // }

                    return {

                        name:'翁宇',

                        age:18

                    };

                }

                var res = fun5();

                console.log(res);

            </script>

    3.全局变量和局部变量

            <script>

                /*

                    全局变量和局部变量

                        全局变量

                            定义:在函数体外部定义的变量,且在函数体内部未采用var声明的变量

                            作用域:在函数体的内部或者外部被访问和修改

                        局部变量

                            定义:在函数体的内部采用var声明的变量

                            作用域:只能在函数体的内部被访问和修改

                    注意:一般将变量定义放在最前面

                 */

                // 全局变量

                var num = 10;

                num = 20;

                // console.log(num)

                function fun(a){

                    num = 50;

                }

                console.log(num);//20

                fun();

                console.log(num);//50

                function demo(a){

                    // 局部变量

                    var age = 18;

                    var num = 30;

                    // 在函数体内部未采用var声明的变量为全局变量

                    sex = '男';

                    console.log(age);

                }

                //报错原因: 函数运行结束后,该变量所占的内存空间则会被释放

                // console.log(age);//age is not defined

                demo();

                // console.log(age);//age is not defined

                console.log(sex);

            </script>

    4.全局变量和局部变量的区别

            <script>

                /*

                    全局变量和局部变量

                    局部变量:

                        避免变量取名问题

                        函数结束后,该变量则释放,则不占内存

                    全局变量:

                        一直保存在内存中,直至整个js运行完毕

                        全局变量会造成变量的污染

                 建议:编程建议使用局部变量

                 */

                // 全局变量

                var num = 10;

                var flag = false;

                function fun(){

                    // 局部变量

                    var age = 20;

                    console.log(age)

                }

                function demo(){

                    var age = 30;

                    console.log(age)

                }

                fun()

                demo()

                // if(){

                //  flag = true;

                // }else{

                //  flag = false;

                // }

            </script>

    5.函数的作用域链

            <script>

                var num1 = 10;

                function fun1(){

                    //对于fun2 全局变量

                    var num2 = 20;

                    function fun2(){

                        // num2对于fun2而言是全局变量

                        console.log(num2);

                        function fun3(){

                            // 局部变量

                            var num2 = 50;

                            console.log(num1);

                            console.log(num2);//50

                        }

                        fun3();

                    }

                    fun2(); 

                }

                fun1();

                /*

                    当局部变量和全局变量冲突时,优先使用局部变量

                    作用域链:函数会一级一级的向上查找变量,直到找到为止,反之不成立

                 */

            </script>

    6.全局函数和局部函数

            <script>

                // 外部函数

                function outside(){

                    var num = 10;

                    console.log(num)

                    // inside()

                    // 内部函数

                    function inside(){

                        var res=num+5;

                        console.log(res)

                    }

                }

                outside();

                inside();// 报错inside is not defined

            </script>

    7.闭包函数

            <script>

                /*

                    闭包函数:当内部函数在其作用域之外被调用则形成了内部函数的闭包

                    闭包函数:

                        优点:可以让内部函数在作用域外被调用

                        缺点:闭包函数占内存,且可能引起内存泄漏

                        解决方案:在闭包结束时将变量销毁(赋值为空)

                 */

                // 外部函数

                function outside(){

                    var num = 10;

                    console.log(num);

                    // 内部函数

                    var inside = function(){

                        var sum = num + 5;

                        num++;//11  12  13  14

                        console.log(sum)

                        // 销毁变量

                        // num = undefined;

                    }

                    return inside;

                }

                var res = outside();

                // console.log(res);

                res();

                outside()

                res();

                outside()

                res();

                outside()

                res();

                // inside();// inside is not defined

            </script>

    相关文章

      网友评论

          本文标题:javascript入门4(0801)

          本文链接:https://www.haomeiwen.com/subject/owdkdctx.html