閉包和定時(shí)器

題目1:下面的代碼輸出多少?修改代碼讓fnArri 輸出 i。使用兩種以上的方法

var fnArr = []; 
for (var i = 0; i < 10; i ++) { 
        fnArr[i] = function(){
        return i; 
    };
} 
//輸出10,執(zhí)行的時(shí)候i等于10,所以整個(gè)數(shù)組元素執(zhí)行后都會(huì)是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:封裝一個(gè)汽車對(duì)象,可以通過如下方式獲取汽車狀態(tài)


var Car = (function () { 
    let speed = 0; 
    function setSpeed(s){ 
        return speed = s;
    }
    function getSpeed(){ 
        return speed; 
    } 
    function accelerate(){ 
        return speed+=10; 
    } 
    function decelerate(){ 
        //速度不能為負(fù)數(shù) 
        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(){ //速度不能為負(fù)數(shù)
        return speed>0?speed-=10:speed;
    },
    getStatus:function(){
        return speed>0?'running':'stop';
    },
};

題目3:下面這段代碼輸出結(jié)果是? 為什么?

var a = 1;
setTimeout(function(){ 
    a = 2;
    console.log(a);//2
}, 0); //參數(shù)為0,被放入執(zhí)行隊(duì)列的最后
var a ;
console.log(a); //1
a = 3;
console.log(a); //3

結(jié)果:1,3,2

題目4:下面這段代碼輸出結(jié)果是? 為什么?

var flag = true;
setTimeout(function(){
    //等待所有任務(wù)結(jié)束后執(zhí)行 
    flag = false;
},0)
while(flag){} //setTimeout會(huì)等待它執(zhí)行完畢,此時(shí)flag永遠(yuǎn)是true,無限循環(huán)。
console.log(flag); //不會(huì)執(zhí)行

題目5:下面這段代碼輸出?如何輸出delayer: 0, delayer:1...
(使用閉包來實(shí)現(xiàn))

for(var i=0;i<5;i++){ 
(function(t){
    //參數(shù)變量提升 let t; 
    return setTimeout(function(){ 
            console.log('delayer:' + t );
        }, 0);
 })(i); 
 
console.log(i);
}```
題目6: 如何獲取元素的真實(shí)寬高?

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';
//如果我們想編碼一個(gè)URL并且可以使用它(訪問),使用encodeURI();
let simpleURL = encodeURI(myURL); //"https://www.google.com/#q=javascript"
//如果我們想編碼一個(gè)URL并且可以將其放置在某URL的參數(shù)中,使用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); //將會(huì)打開一個(gè)窗口,地址為https://www.google.com/#q=javascript


題目8:補(bǔ)全如下函數(shù),判斷用戶的瀏覽器類型

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容