zoukankan      html  css  js  c++  java
  • 查缺补漏

    查缺补漏

    1. 函数声明提升

    function test() {
        foo(); // TypeError "foo is not a function"
        bar(); // "this will run!"
        var foo = function () { // function expression assigned to local variable 'foo'
            alert("this won't run!");
        }
        function bar() { // function declaration, given the name 'bar'
            alert("this will run!");
        }
    }
    test();

    实际上是这样的,

    function test() {
        var foo;
        var bar;
        bar = function () { // function declaration, given the name 'bar'
            alert("this will run!");
        }
    
        foo(); // TypeError "foo is not a function"
        bar(); // "this will run!"
    
        foo = function () { // function expression assigned to local variable 'foo'
            alert("this won't run!");
        }
    }
    test();

    2. 充分利用 Object.prototype.toString.call();方法,你会发现他是多么强大!

    3. 命名函数表达式中的函数名只对内有效,对外是看不见的。

    var f = function foo(){
       return typeof foo; // foo是在内部作用域内有效
     };
     // foo在外部用于是不可见的
     typeof foo; // "undefined"
     f(); // "function"

    4. 合理使用try catch , 他不会影响代码的正常运行。

        try {
            console.log(a);
        } catch (e){
            console.log(e);
        }
        var b = 10;
        b += 50;
        console.log(b);

    5. 注意 DOMContentLoaded和load的区别,前者是针对document而言的, 渲染树生成即触发。 而后者是针对window的,需要所有的资源都下载结束才触发。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>fd.html</title>
    </head>
    <body>
        <img src="http://u.candou.com/2015/0906/1441528903323.jpg" alt="">
        <img src="http://lovelace-media.imgix.net/uploads/812/3a94d580-7e7a-0133-9f0b-0af7184f89fb.gif?" alt="">
        <div class="dfa"></div>
        <script>
            console.time("time");
            function show(str) {
                console.log(str);
                console.timeEnd("time");
                console.log("
    ");
            }
            show("1.直接调用show");
    
    
            document.addEventListener('DOMContentLoaded',function () {
                show("2.DOMContentLoaded");
            }, false);
    
            window.addEventListener('load',function () {
                show("3. load");
            }, false);
    
            show("4. 直接调用show");
    
        </script>
    </body>
    </html>

    结果如下;

    6.

  • 相关阅读:
    Java程序员:一整个项目的具体开发流程介绍
    JAVA常用API整理
    Java开发人员必知必会的20种常用类库和API
    SpringBoot_web开发【实验】-员工列表-链接高亮&列表完成
    luogu P1754 球迷购票问题 |动态规划
    luogu P1566 加等式 |背包问题方案数
    luogu P1564 膜拜 |动态规划
    luogu P1509 找啊找啊找GF |背包
    P1474 货币系统 Money Systems |背包方案数
    cloudera安装报错 socket.gaierror: [Errno -2] Name or service not known
  • 原文地址:https://www.cnblogs.com/zhuzhenwei918/p/6657954.html
Copyright © 2011-2022 走看看