image

编辑人: 独留清风醉

calendar2025-05-31

message2

visits925

小米的两道 js 面试题

一、

请定义这样一个函数

function repeat (func, times, wait) {

}

这个函数能返回一个新函数,比如这样用

var repeatedFun = repeat(alert, 10, 5000)

调用这个 repeatedFun (“hellworld”)

会alert十次 helloworld, 每次间隔5秒

 

function repeat(func, times, wait) {
    function myRepeat() {
            var _arguments = arguments,
            i = 0;
            var handle = setInterval(function () {
                ++i;
                if (i === times) {
                    clearInterval(handle);
                    return;
                }
                func.apply(null,_arguments);
            },wait)
    }
    return myRepeat;
}
var repeatFun = repeat(alert,10,5000);
repeatFun('hello world')

二、

写一个函数stringconcat, 要求能

var result1 = stringconcat(“a”, “b”) result1 = “a+b”

var stringconcatWithPrefix = stringconcat.prefix(“hellworld”);

var result2 = stringconcatWithPrefix(“a”, “b”) result2 = “hellworld+a+b”

喵呜刷题:让学习像火箭一样快速,快来微信扫码,体验免费刷题服务,开启你的学习加速器!

创作类型:
原创

本文链接:小米的两道 js 面试题

版权声明:本站点所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明文章出处。
分享文章
share