• 函数声明和函数表达式


    function foo(){

        function bar() {

            return 3;

        }

        return bar();

        function bar() {

            return 8;

        }

    }

    alert(foo());

    题 2:

    function foo(){

        var bar = function() {

            return 3;

        };

        return bar();

        var bar = function() {

            return 8;

        };

    }

    alert(foo());

    题 3:

    alert(foo());

    function foo(){

        var bar = function() {

            return 3;

        };

        return bar();

        var bar = function() {

            return 8;

        };

    }

    题 4:

    function foo(){

        return bar();

        var bar = function() {

            return 3;

        };

        var bar = function() {

            return 8;

        };

    }

    alert(foo());

    答案是8、3、3和 [Type Error: bar is not a function]

    分析:

    //**Simulated processing sequence for Question 1**

    function foo(){

        //define bar once

        function bar() {

            return 3;

        }

        //redefine it

        function bar() {

            return 8;

        }

        //return its invocation

        return bar(); //8

    }

    alert(foo());

    return 语句后面的代码是运行不到的啊……

    //**Simulated processing sequence for Question 2**

    function foo(){

        //a declaration for each function expression

        var bar = undefined;

        var bar = undefined;

        //first Function Expression is executed

        bar = function() {

            return 3;

        };

        // Function created by first Function Expression is invoked

        return bar();

        // second Function Expression unreachable

    }

    alert(foo()); //3

    //**Simulated processing sequence for Question 4**

    function foo(){

        //a declaration for each function expression

        var bar = undefined;

        var bar = undefined;

        return bar(); //TypeError: "bar not defined"

        //neither Function Expression is reached

    }

    alert(foo());

  • 相关阅读:
    java把集合数据写入txt文档
    eclipse 启动报内存溢出的问题out of memory!
    面向对象基础知识整理
    在不同环境下MD5加密相同字符串,密文不一样的问题
    氮化镓选型
    碳化硅选型
    DSP选型
    FPGA选型
    MCU选型
    正点原子
  • 原文地址:https://www.cnblogs.com/lucybloguniquecom/p/5807127.html
Copyright © 2020-2023  润新知