JS闭包_定时器_BOM

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 ++) {
      fnArr[i]=function(){
        var n=i;
        return function(){
         return n;
        }
      }();
    }
    console.log( fnArr[3]() );
    var fnArr = [];
    for (var i = 0; i < 10; i++) {
      (function (n) {
       fnArr[i] = function(){
         return n;
       }
      })(i);
    }
    console.log(fnArr[3]());   

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

var Car = (function(){
   var speed = 0;
   function setSpeed(s){
       speed = s
   }
   ...
   return {
      setSpeed: setSpeed,
      ...
   }
})()
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';
var Car = (function(){
   var speed = 0;
   function setSpeed(s){
       speed = s
   }
   function getSpeed(){
     console.log(speed);
   }
   function accelerate(){
     speed+=10;
   }
   function decelerate(){
     speed-=10;
   }
   function getStatus(){
     if(speed>0){
       console.log("running");
     }else{
       console.log("stop");
     }
   }
   return {
      setSpeed: setSpeed,
      getSpeed: getSpeed,
      accelerate: accelerate,
      decelerate: decelerate,
      getStatus: getStatus
   }
})()
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';

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
定时器是异步执行会在队列最尾处等同步执行后再执行,最先输出1和3,定时器中的函数a使用的是全局变量,将a赋值为2输出

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

var flag = true;
setTimeout(function(){
   flag = false;
},0)
while(flag){}
console.log(flag);

无法输出,报错
因为定时器异步执行是等同步执行后才执行的,但flag永远为true,陷入了死循环,报错。

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(x){
    setTimeout(function(){
      console.log("delayer:"+x);
    },0)
  })(i)
    console.log(i);
}

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

var style = window.getComputedStyle("元素", "伪类");
var div=$("#tab");
var width=getComputedStyle(div,null)["width"];
var height =getComputedStyle(div,null)["height"];

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

编码:encodeURI() encodeURIComponent()
解码:decodeURI() decodeURIComponent()
编码是为了避免解析网址时产生歧义

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 /iPhone|iPad/.test(navigator.userAgent);
}

你可能感兴趣的:(JS闭包_定时器_BOM)