美文网首页
闭包_定时器_BOM

闭包_定时器_BOM

作者: QQQQQCY | 来源:发表于2017-07-20 22:26 被阅读0次

    题目1: 下面的代码输出多少?修改代码让 fnArr[i]() 输出 i。使用 两种以上的方法

        var fnArr = [];
        for (var i = 0; i < 10; i ++) {
            fnArr[i] =  function(){
                return i;
            };
        }
        console.log( fnArr[3]() );  //
    
    输出
    10
    
    //  方法一
     var fnArr = [];
        for (var i = 0; i < 10; i ++) {
          !function (i){
            fnArr[i] =  function(){
                return i;
            }
          }(i);
        }
        console.log( fnArr[3]() );  //  输出  3
    
    //  方法二
    var fnArr = [];
        for (var i = 0; i < 10; i ++) {
          fnArr[i] = function fn1(i){
            function fn2(){
              return i
            }
            return fn2;
          }(i);
        }
        console.log( fnArr[3]() );  //  输出  3
    

    题目2: 封装一个汽车对象,可以通过如下方式获取汽车状态

    var Car = (function(){
       var speed = 0;
       function setSpeed(s){
           speed = s
       }
       function getSpeed(){
         console.log(speed);
         return speed;
       }
      function accelerate(){
        speed += 10;
      }
      function decelerate(){
        speed -= 10;
        speed = Math.max(0,speed);
      }
      
      function getStatus(){
        if (speed) { 
          console.log('running');
        } else {
          console.log('stop');
        }
      }
       return {
          setSpeed: setSpeed,
          getSpeed: getSpeed,
          accelerate: accelerate,
          decelerate: decelerate,
          getStatus: getStatus,
          speed:"error"
       }
    })()
    Car.setSpeed(30);
    Car.getSpeed(); //30
    Car.accelerate();
    Car.getSpeed(); //40;
    Car.decelerate();
    Car.decelerate();
    Car.getSpeed(); //20
    Car.getStatus(); // 'running';
    Car.decelerate(); 
    Car.decelerate();
    Car.getStatus();  //'stop';
    Car.speed;  //error
    

    题目3:下面这段代码输出结果是? 为什么?

    var a = 1;
    setTimeout(function(){
        a = 2;
        console.log(a);
    }, 0);
    var a ;
    console.log(a);
    a = 3;
    console.log(a);
    
    输出
    1
    3
    2
    

    因为 setTimeout运行机制是,将指定的代码移出本次执行,等到下一轮Event Loop时,再检查是否到了指定时间。如果到了,就执行对应的代码;如果不到,就等到再下一轮Event Loop时重新判断。这意味着,setTimeout指定的代码,必须等到本次执行的所有代码都执行完,才会执行。
    所以a的赋值顺序为1,3,2

    题目4:下面这段代码输出结果是? 为什么?

    var flag = true;
    setTimeout(function(){
        flag = false;
    },0)
    while(flag){}
    console.log(flag);
    
    无输出
    因为一直持续while循环,不会跳出
    

    题目5: 下面这段代码输出?如何输出delayer: 0, delayer:1...(使用闭包来实现)

    for(var i=0;i<5;i++){
        setTimeout(function(){
             console.log('delayer:' + i );
        }, 0);
        console.log(i);
    }
    
    输出
    0
    1
    2
    3
    4
    delayer:5
    delayer:5
    delayer:5
    delayer:5
    delayer:5
    
    //  修改
    for(var i=0;i<5;i++){
        function outFun(i){
          function fun(){
             console.log('delayer:' + i );
          }
          return fun
        }
        setTimeout(outFun(i), 0);
    }
    

    题目6: 如何获取元素的真实宽高

     function getCss(e,attr){
       return  (e.currentStyle ? e.currentStyle : window.getComputedStyle(e,null))[attr]
     }
     var e = document.querySelector('.test');
     console.log(getCss(e,'height'));
     console.log(getCss(e,'width'));
    

    题目7: URL 如何编码解码?为什么要编码?

    • URL 如何编码解码

    JavaScript提供四个URL的编码/解码方法。

    1. decodeURI()
    2. decodeURIComponent()
    3. encodeURI()
      4encodeURIComponent()

    区别
    encodeURI方法不会对下列字符编码

    ASCII字母
    数字
    ~!@#$&*()=:/,;?+'
    

    encodeURIComponent方法不会对下列字符编码

    ASCII字母
    数字
    ~!*()'
    

    所以encodeURIComponent比encodeURI编码的范围更大。
    实际例子来说,encodeURIComponent会把 http:// 编码成 http%3A%2F%2F 而encodeURI却不会。

    如果你需要编码整个URL,然后需要使用这个URL,那么用encodeURI。

    encodeURI(http://www.cnblogs.com/season-huang/some other thing);
    
    http://www.cnblogs.com/season-huang/some%20other%20thing;
    

    其中,空格被编码成了%20。但是如果你用了encodeURIComponent,那么结果变为

    "http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2Fsome%20other%20thing"
    

    当你需要编码URL中的参数的时候,那么encodeURIComponent是最好方法。

    var param = "http://www.cnblogs.com/season-huang/"; //param为参数
    param = encodeURIComponent(param);
    var url = "http://www.cnblogs.com?next=" + param;
    console.log(url) //"http://www.cnblogs.com?next=http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2F"
    

    参数中的 "/" 可以编码,如果用encodeURI肯定要出问题,因为后面的/是需要编码的

    • 为什么要编码

    在计算机内部,所有的信息最终都表示为一个二进制的字符串。每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte)。也就是说,一个字节一共可以用来表示256种不同的状态,每一个状态对应一个符号,就是256个符号,从0000000到11111111。

    上个世纪60年代,美国制定了一套字符编码,对英语字符与二进制位之间的关系,做了统一规定。这被称为ASCII码,一直沿用至今。

    英语用128个符号编码就够了,但是其他国家文化的文字符号,远远没法包含,所以有了将世界上所有的符号都纳入其中的Unicode编码

    为了储存的优化,又有了UTF-8

    一般来说,URL只能使用英文字母、阿拉伯数字和某些标点符号,不能使用其他文字和符号。

    意味着,如果URL中有其他字符,就必须编码后使用

    不同的操作系统、不同的浏览器、不同的网页字符集,将导致完全不同的编码结果。
    所以用Javascript先对URL编码,再向服务器提交,保证服务器得到的数据是格式统一的。

    题目8: 补全如下函数,判断用户的浏览器类型

    function isAndroid(){
        return /Android/.test(navigator.userAgent);
    }
    function isIphone(){
        return /iPhone/.test(navigator.userAgent);
    }
    function isIpad(){
        return /iPad/.test(navigator.userAgent);
    }
    function isIOS(){
        return /(iPad)|(iPhone)/i.test(navigator.userAgent);
    }
    

    相关文章

      网友评论

          本文标题:闭包_定时器_BOM

          本文链接:https://www.haomeiwen.com/subject/uvpdkxtx.html