zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    MVC3、如何应用EntityFramework 连接MySql 数据库 Kevin
    DEV EXPRESS Summary Footer 不显示 Kevin
    装饰模式 Kevin
    Dev 控件 GridControl 控件 二次绑定数据源的问题。 Kevin
    System.InvalidOperationException 异常 Kevin
    LINQ to XML Kevin
    代理模式——代码版“吊丝的故事” Kevin
    VS2012 中的设备 面板 Kevin
    maven 学习笔记(三)创建一个较复杂的 eclipse+android+maven 工程
    maven 学习笔记(一)eclipse+android+maven
  • 原文地址:https://www.cnblogs.com/litings/p/8151331.html
Copyright © 2011-2022 走看看