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>
  • 相关阅读:
    微信转发或分享朋友圈带缩略图、标题和描述的实现方法
    apache一个IP多个站点的配置方法
    微信网页扫码登录的实现
    laravel take(3) 读取最近三条信息
    微信卡劵、微信卡包,必须是认证订阅号或认证服务号
    CSS3 去除苹果浏览器按钮input[type="submit"]和input[type="reset"]的默认样式
    使用laravel5.4结合easywechat进行微信开发--基本配置
    Class 'QrCode' not found ? 和 laravel 生成二维码接口(Simple QrCod)
    windows redis的启动 和 Laravel中Redis的使用
    改变checkbox的默认样式
  • 原文地址:https://www.cnblogs.com/mengfangui/p/9356242.html
Copyright © 2011-2022 走看看