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

  • 相关阅读:
    JavaScript异步编程1——Promise的初步使用
    Pailler
    ElGamal
    RSA
    密码基础
    博客园中:为文章添加版权保护
    DCT实现水印嵌入与提取(带攻击)
    量子:基于EPR块对的两步量子直接通信
    量子:拜占庭协议和测谎问题的量子协议的实验证明
    liunx:网络命令
  • 原文地址:https://www.cnblogs.com/lucybloguniquecom/p/5807127.html
Copyright © 2011-2022 走看看