小米Web前端JavaScript面試題

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   sp   java   for   on   

面試題目

 

一、

 

請定義這樣一個函數

 

function repeat (func, times, wait) {

 

}

 

這個函數能返回一個新函數,比如這樣用

 

var repeatedFun = repeat(alert, 10, 5000)

 

調用這個 repeatedFun ("hellworld")

 

會alert十次 helloworld, 每次間隔5秒

 

二、

 

寫一個函數stringconcat, 要求能

var result1 = stringconcat("a", "b")  result1 = "a+b"

 

var stringconcatWithPrefix = stringconcat.prefix("hellworld");

 

var result2 = stringconcatWithPrefix("a", "b")  result2 = "hellworld+a+b"

 

小菜解法

 

     這兩道題,考的就是閉包,廢話不多說,直接上代碼。

 1 /** 2  * 第一題 3  * @param func 4  * @param times 5  * @param wait 6  * @returns {repeatImpl} 7  */ 8 function repeat (func, times, wait) { 9     //不用匿名函數是為了方便調試10     function repeatImpl(){11         var handle,12             _arguments = arguments,13             i = 0;14         handle = setInterval(function(){15             i = i + 1;16             //到達指定次數取消定時器17             if(i === times){18                 clearInterval(handle);19                 return;20             }21             func.apply(null, _arguments);22         },wait);23     }24 25     return repeatImpl;26 }27 28 //測試案例29 var repeatFun = repeat(alert, 4, 3000);30 31 repeatFun("hellworld");32 33 34 /**35  * 第二題36  * @returns {string}37  */38 function stringconcat(){39     var result = [];40 41     stringconcat.merge.call(null, result, arguments);42     return result.join("+");43 }44 45 stringconcat.prefix = function(){46     var _arguments = [],47         _this = this;48 49     _this.merge.call(null, _arguments, arguments);50 51     return function(){52         var _args = _arguments.slice(0);53 54         _this.merge.call(null, _args, arguments);55         return _this.apply(null, _args);56     };57 };58 59 stringconcat.merge = function(array, arrayLike){60     var i = 0;61 62     for(i = 0; i < arrayLike.length; i++){63         array.push(arrayLike[i]);64     }65 }66 67 68 //測試案例69 var result1 = stringconcat("a", "b"); //result1 = "a+b"70 var result3 = stringconcat("c", "d"); //result1 = "a+b"71 72 var stringconcatWithPrefix = stringconcat.prefix("hellworld");73 var stringconcatWithPrefix1 = stringconcat.prefix("hellworld1");74 75 var result2 = stringconcatWithPrefix("a", "b"); //result2 = "hellworld+a+b"76 var result4 = stringconcatWithPrefix1("c", "d"); //result2 = "hellworld+a+b"77 78 alert(result1);79 alert(result2);80 alert(result3);81 alert(result4);

 

小米Web前端JavaScript面試題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.