闭包和定时器

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

var fnArr = []; 
for (var i = 0; i < 10; i ++) { 
        fnArr[i] = function(){
        return i; 
    };
} 
//输出10,执行的时候i等于10,所以整个数组元素执行后都会是10
console.log( fnArr[3]() ); 

作用域链:

globalContext = {
    AO:{
          fnArr:[function1,function2,...,function10] 
          i:10
          function1:
          function2:
          ...
          function10:
    }
    scope:null
}
function1.[[scope]] = globalContext.AO;
function2.[[scope]] = globalContext.AO;


function1Context = {
    AO:{
    }
    scope:function1.[[scope]];//globalContext.AO;
}
function2Context = {
    AO:{
    }
    scope:function2.[[scope]];//globalContext.AO;
}

法一:

var fnArr = []; 
for (var i = 0; i < 10; i ++) { 
       (function(i){ 
             fnArr[i] = function(){ 
                   return i; 
             } 
      })(i); 
}
console.log( fnArr[3]() ); //3

作用域链:

globalContext = {
    AO:{
          fnArr:[function11,function22,...,function1010]
          i:
          function1:
          function2:
          ...
          function10:
    }
    scope:null
}
function1.[[scope]] = globalContext.AO;
function2.[[scope]] = globalContext.AO;


function1Context = {
    AO:{
        i:0;
        function11:
    }
    scope:function1.[[scope]];//globalContext.AO;
}
function11.[[scope]] = function1Context.AO;
function11Context = {
    AO:{
    }
    scope:function11.[[scope]];//function1Context.AO;
}

function2Context = {
    AO:{
         i:1;
        function22:
    }
    scope:function2.[[scope]];//globalContext.AO;
}
function22.[[scope]] = function2Context.AO;
function22Context = {
    AO:{
    }
    scope:function22.[[scope]];//function2Context.AO;
}

法二:

var fnArr = []; 
for (var i = 0; i < 10; i ++) {     
    fnArr[i] = (function(i){ 
    //IIFE & 闭包 
          return function(){ 
                return i;           
        }       
    })(i);  
} 
console.log( fnArr[3]() ); //3

作用域链:

globalContext = {
    AO:{
          fnArr:[function1,function2,...,function10]
          i:
          function1:
          function2:
          ...
          function10:
    }
    scope:null
}
function1.[[scope]] = globalContext.AO;
function2.[[scope]] = globalContext.AO;


function1Context = {
    AO:{
        i:0;
        function11:
    }
    scope:function1.[[scope]];//globalContext.AO;
}
function11.[[scope]] = function1Context.AO;
function11Context = {
    AO:{
    }
    scope:function11.[[scope]];//function1Context.AO;
}

function2Context = {
    AO:{
         i:1;
        function22:
    }
    scope:function2.[[scope]];//globalContext.AO;
}
function22.[[scope]] = function2Context.AO;
function22Context = {
    AO:{
    }
    scope:function22.[[scope]];//function2Context.AO;
}

法三:

var fnArr = [];
 for (let i = 0; i < 10; i ++) {
 //使用ES6: let 
        fnArr[i] = function(){ 
              return i;
        }; 
} 
console.log( fnArr[3]() );

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


var Car = (function () { 
    let speed = 0; 
    function setSpeed(s){ 
        return speed = s;
    }
    function getSpeed(){ 
        return speed; 
    } 
    function accelerate(){ 
        return speed+=10; 
    } 
    function decelerate(){ 
        //速度不能为负数 
        return speed>0?speed-=10:speed; 
    } 
    function getStatus(){ 
        return speed>0?'running':'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';
//Car.speed; //error

代码:

var Car = {
    speed:0,
    setSpeed:function(s){
        return speed = s;
    },
    getSpeed:function(){
        return speed;
    },
    accelerate:function(){
        return speed+=10;
    },
    decelerate:function(){ //速度不能为负数
        return speed>0?speed-=10:speed;
    },
    getStatus:function(){
        return speed>0?'running':'stop';
    },
};

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

var a = 1;
setTimeout(function(){ 
    a = 2;
    console.log(a);//2
}, 0); //参数为0,被放入执行队列的最后
var a ;
console.log(a); //1
a = 3;
console.log(a); //3

结果:1,3,2

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

var flag = true;
setTimeout(function(){
    //等待所有任务结束后执行 
    flag = false;
},0)
while(flag){} //setTimeout会等待它执行完毕,此时flag永远是true,无限循环。
console.log(flag); //不会执行

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

for(var i=0;i<5;i++){ 
(function(t){
    //参数变量提升 let t; 
    return setTimeout(function(){ 
            console.log('delayer:' + t );
        }, 0);
 })(i); 
 
console.log(i);
}```
题目6: 如何获取元素的真实宽高?

function trueStyle(element,pseduoElement){
//IE不支持window.getComputedStyle(),支持element.currentStyle();
return element.currentStyle ? element.currentStyle : window.getComputedStyle(element,pseduoElement);
}
let trueWidth = trueStyle(element).width;
let trueHeight = trueStyle(element).height;

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

let myURL = 'https://www.google.com/#q=javascript';
//如果我们想编码一个URL并且可以使用它(访问),使用encodeURI();
let simpleURL = encodeURI(myURL); //"https://www.google.com/#q=javascript"
//如果我们想编码一个URL并且可以将其放置在某URL的参数中,使用encodeURIComponent();
let completeURL = encodeURIComponent(myURL);
let newURL = 'https://www.google.com/?back=' + completeURL;
//"https://www.google.com/?back=https%3A%2F%2Fwww.google.com%2F%23q%3Djavascript"
window.open(simpleURL); //将会打开一个窗口,地址为https://www.google.com/#q=javascript


题目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);
}

你可能感兴趣的:(闭包和定时器)