zoukankan      html  css  js  c++  java
  • 函数声明和函数表达式

    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());

  • 相关阅读:
    vue自定义指令
    ZOJ Problem Set–2104 Let the Balloon Rise
    ZOJ Problem Set 3202 Secondprice Auction
    ZOJ Problem Set–1879 Jolly Jumpers
    ZOJ Problem Set–2405 Specialized FourDigit Numbers
    ZOJ Problem Set–1874 Primary Arithmetic
    ZOJ Problem Set–1970 All in All
    ZOJ Problem Set–1828 Fibonacci Numbers
    要怎么样调整状态呢
    ZOJ Problem Set–1951 Goldbach's Conjecture
  • 原文地址:https://www.cnblogs.com/lucybloguniquecom/p/5807127.html
Copyright © 2011-2022 走看看