zoukankan      html  css  js  c++  java
  • js 变量提升与函数提升

    规则:

    函数的提升优先于变量提升。同名的函数会覆盖同名的函数与变量。同名的变量不会覆盖同名的函数。

    示例代码1:

    <!DOCTYPE html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>变量提升与函数提升</title>
    </head>
    <body>
        <script type="text/javascript">
            var a = 20;
            function fn(){
                console.log('fn')
            }
            function fn(){
                console.log('covert fn')
            }
            function a(){
                console.log('cover a')
            }
            console.log(a);
            fn();
            var fn = 'I want to cover function name fn.'
            console.log(fn)
            函数的提升优于变量提升。同名的函数会覆盖函数与变量。
        </script>
    </body>
    </html>

    控制台输出

     

    解释:

    <!DOCTYPE html>
    <html lang="zh">
    
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <title>变量提升与函数提升</title>
        </head>
    
        <body>
            <script type="text/javascript">
                //真正的执行顺序为:
                //函数提升
                function fn() {
                    console.log('fn')
                }
                //函数提升
                function fn() {
                    console.log('covert fn')
                }
                //函数提升
                function a() {
                    console.log('cover a')
                }
                //变量提升
                var a = undefined;
                //变量提升
                var fn = undefined;
                a = 20;
                console.log(a); //20
                fn(); //covert fn
                fn = 'I want to cover function name fn.';
                console.log(fn); //I want to cover function name fn.
            </script>
        </body>
    
    </html>

     示例代码2:

    <!DOCTYPE html>
    <html lang="zh">
    
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <title>变量提升与函数提升</title>
        </head>
    
        <body>
            <script type="text/javascript">
                function test() {
                    console.log(foo)
                    console.log(bar)
                    var foo = 'hello'
                    console.log(foo)
                    var bar = function() {
                        return 'world'
                    }
    
                    function foo() {
                        return 'hello'
                    }
                }
                test();
            </script>
        </body>
    
    </html>

    控制台输出:

    解释:

    <!DOCTYPE html>
    <html lang="zh">
    
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <title>变量提升与函数提升</title>
        </head>
    
        <body>
            <script type="text/javascript">
                //执行顺序为:
                function test() {
                    //函数提升
                    function foo() {
                        return 'hello'
                    }
                    //变量提升 但是不会覆盖同名的函数
                    // var foo = undefined;
                    //变量提升
                    var bar = undefined;
                    console.log(foo)
                    console.log(bar)
                    foo = 'hello'
                    console.log(foo)
                    bar = function() {
                        return 'world'
                    }
                }
                test();
            </script>
        </body>
    
    </html>
  • 相关阅读:
    最小生成树(卡鲁斯卡尔)
    最小生成树(kruskal模版 Prim模板)
    hdu1247(Hat’s Words)
    hdu1671Phone List(字典树)
    hdu1305Immediate Decodability(字典树)
    hdu1251(统计难题)
    oj1500(Message Flood)字典树
    oj2892(字典树)
    数论公式
    2504(多项式求和)
  • 原文地址:https://www.cnblogs.com/mengfangui/p/9356242.html
Copyright © 2011-2022 走看看