• JavaScript 趣味题。


      第一题:

        const Greeters = []
        for (var i = 0 ; i < 10 ; i++) {
            Greeters.push(function () { return console.log(i) })
        }
        Greeters[0]()
        Greeters[1]() 
        Greeters[2]()
      // 运行结果三个 10
      // 解决办法 1、把 var 换成 let, 2、把function(){return console.log(i)} 换成 console.log.bind(null,i)

      第二题:

    var z =10;
    function foo(){
      console.log(z)  
    }
    (function(funArg){
      var z = 20;
      funArg();
    })(foo);
    //结果 10
    //函数调用的位置并不会改变 函数体的位置 所有最后返回的 10

       第三题:

      function puzzle() {
        return ()=> {
          console.log(arguments)
        }
     }
    puzzle('a', 'b', 'c')(1, 2, 3)
    //结果 ["a","b","c"]
    //在箭头函数中,this与封闭词法上下文的this保持一致。在全局代码中,它将被设置为全局对象:

      第三题

    function fun(n,o){
      console.log(o)
      return{
        fun:function(m){
          return fun(m,n);
        }
      };
    }
    var a = fun(0); a.fun(1); a.fun(2); a.fun(3);
    var b = fun(0).fun(1).fun(2).fun(3);
    var c = fun(0).fun(1); c.fun(2); c.fun(3);

      //undefined 0 0 0
      //undefined 0 1 2
      //undefined 0 1 1

      第四题

    var a = 100;
    function testResult(){
      var b = 2 * a;
      var a = 200;
      var c = a / 2;
      alert(b);
      alert(c);
    }
    testResult();
    // NaN,100
    // 变量 a 在函数内变量提升 所以在一开始 a的值是 underfind 所以 b= 2*a=NaN

       第五题

    var test = (function(a){
      this.a = a;
      return function(b){
        return this.a + b;
      }
    }(function(a,b){
      return a;
    }(1,2)));
    console.log(test(1));
    // 2

      第六题

    (function(){
      var a = b = 3;
    })();
    console.log("a defined?" + (typeof a != 'undefined'));
    console.log("a defined?" + (typeof b != 'undefined'));

    //a defined?false
    //a defined?true

    // 主要是变量 b 没有使用var 关键字进行声明 所有导致变量 b 变成全局变量

       第七题

    (function(){
        console.log(1);
        setTimeout(function(){console.log(2)},1000);
        setTimeout(function(){console.log(3)},0);
        console.log(4);
    })();
    // 1,4,3,2
    // 单线程 异步执行

      第八题

    if(!("a" in window)){
        var a = 1;
    }
    alert(a);
    // undefined
    // = = ......

      第九题

    var handle = function(a){
        var b = 3;
        var tmp = function(a){
            b = a + b; 
            return tmp;
        }
        tmp.toString= function(){
            return b;
        }
            return tmp;
    } alert(handle(
    4)(5)(6));
    // 14

     第十题

    var arr = [1,'abc',function(){alert(3333);}];
    alert(arr[2]());
    arr[2]();
    // 先弹出 3333 再弹出 underfind 最后弹出 3333

    第十一题

    var len = 4;
    while(len--){
            setTimeout(function(){console.log(len)},3000);
            console.log(len);
    }
    // 3 2 1 0 -1

    第十二题

    window.name = "Window";
    var cat = {
            name:'Cat'
    };
    var dog = {
            name:'Dog',
            sound:function(word){
                alert(this.name + word);
            }
    };
    dog.sound(" is pooping");
    dog.sound.call(window," is banking");
    dog.sound.call(dog," is banking");
    dog.sound.apply(cat,[ 'hello']);
    // Dog is pooping
    // Window is banking
    // Dog is banking
    // Cat hello

    第十三题

    for(var i = 0,j = 0; i < 10, j < 6; i++, j++){
    value = i + j;
    }
    alert(value);
    //10

    第十四题

    alert(0/0);
    alert(1/0);
    alert(0/1);
    // NaN Infinity 0

    第十五题

    var foo = {n: 1};
    var bar = foo;
    foo.x = foo = {n: 2};
    console.log(foo.x);
    //undefined
  • 相关阅读:
    vue 相对其他热门 框架 优点 --- 待续
    vue router 只需要这么几步
    正则表达式
    MySQL数据库优化的八种方式
    Django REST Framework 最佳实践
    Node.js ZLIB
    Node.js 虚拟机
    Node.js 实用工具
    Node.js URL
    Node.js UDP/Datagram
  • 原文地址:https://www.cnblogs.com/litings/p/8151331.html
Copyright © 2020-2023  润新知