打赏

JavaScript 闭包 面试题

<!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>javascript 闭包 面试题</title>
    </head>

    <body>
        <script>
            function fn(n, o) {
                console.log(o);
                return {
                    fn: function(m) {
                        return fn(m, n);
                    }
                };
            }
            var a = fn(0); //undefined
            a.fn(1); //0
            a.fn(2); //0
            a.fn(3); //0
            console.log('------------');
            var b = fn(0).fn(1).fn(2).fn(3); //undefined,0,1,2
            console.log('------------');
            var c = fn(0).fn(1); //undefined,0
            c.fn(2); //1
            c.fn(3); //1
        </script>
    </body>

</html>
posted @ 2018-03-27 11:53  孟繁贵  阅读(271)  评论(0编辑  收藏  举报
TOP