JS二

JS经典基础题型

<script>
        var i = 5;
        function fn(i) {
            return function (n) {
                console.log(n + (++i));
            }
        }
        var f = fn(1);
        f(2); // 4
        fn(3)(4); // 8
        fn(5)(6); // 12
        f(7); //  10
        console.log(i); // 5
    </script>
<script>
        var i = 20;
        function fn() {
            i -= 2;
            var i = 10;
            return function (n) {
                console.log((++i) - n);
            }
        }
        var f = fn();
        f(1); // 10
        f(2); // 10
        fn()(3); // 8 新开辟一个执行上下文
    </script>
<script>
        let x = 5;
        function fn(x) {
            return function (y) {
                console.log(y + (++x));
            }
        }
        let f = fn(6);
        f(7);// 14
        fn(8)(9);// 18
        f(10);//18
        console.log(x);// 5
    </script>
<script>
        let x = 5;
        function fn() {
            return function (y) {
                console.log(y + (++x));
            }
        }
        let f = fn(6);
        f(7); // 13
        fn(8)(9); // 16 
        f(10); // 18
        console.log(x); // 8
    </script>
<script>
        let a = 0;
        b = 0;
        function A(a) {
            A = function (b) {
                alert(a + b++)
            }
            alert(a++)
        }
        A(1); // 1
        A(2); // 4
    </script>
<script>
        var t = (function (i) {
            return function () {
                alert(i *= 2); 
            }
        })(2);
        t(5); // 4
    </script>
<script>
        var n = 0;
        function a() {
            var n = 10;
            function b() {
                n++;
                console.log(n);
            }
            b(); // 11
            return b;
        }
        var c = a();
        c(); // 12
        console.log(n); // 0
    </script>
<script>
    var a = 1;
    function fn(a) {
        // EC(fn)
        //    1)形参的赋值   2)提升
        //    AO   a = 1   a = 函数   a = 2
        console.log(a);
        var a = 2;
        function a() { console.log("ok") }
        console.log(a);  // 2
    }
    fn(a);
    console.log(a);   // 1
</script>
<script>
        var a = 1;
        function b() {
            if (!a) {
                var a = 2;
            }
            console.log(a) // 2
        }
        b();
    </script>
<script>
        var a = 10,
            b = 11,
            c = 12;
        function f(a) {
            a = 1;
            var b = 2;
            c = 3;
        }
        f(10);
        console.log(a); //10
        console.log(b); //11
        console.log(c); //3
    </script>
<script>
        if (!('a' in window)) {
            var a = 110;
        }
        console.log(a); //undefined
    </script>
<script>
        let a = 0,
            b = 0;

        function A(a) {
            A = function (b) {
                alert(a + b++)
            }
            alert(a++)
        }
        A(1)
        A(2)
        // 1  4
    </script>
<script>
        let a = 0,
            b = 0;

        function A(a) {
            // 此时这个A是函数内部的局部变量
            var A = function (b) {
                alert(a + b++)
            }
            alert(a++)
        }
        A(1)
        A(2)
        // 1 2
    </script>
<script>
        let a = 0,
            b = 0;
        function A(a) {
            B = function (b) {
                alert(a + b++)
            }
            alert(a++)
         }
         A(1)
         B(2)
         // 1  4
     </script>
<script>
         let a = 0,
             b = 0;
         function A(a) {
             A = function (b) {
                 alert(a + b++)
             }
             alert(a++)
             return A;
         }
         A(1)(2)
         // 1 4 
     </script>
<script>
          g();
    console.log(a);  //undefined
    var a = 110;
    function g() {
        k();
        console.log(b);//undefined
        console.log("g...")//g...
        var b = 2;
        console.log(b)//2

        function k() {
            console.log("k...");//k...
        }
    }
     </script>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值