Home > Web Front-end > JS Tutorial > Xiaomi JavaScript interview questions_javascript skills

Xiaomi JavaScript interview questions_javascript skills

WBOY
Release: 2016-05-16 16:23:41
Original
1854 people have browsed it

Interview questions

1.

Copy code The code is as follows:

Please define such a function
function repeat (func, times, wait) {
}
This function can return a new function, for example, use
like this var repeatedFun = repeat(alert, 10, 5000)
Call this repeatedFun ("hellworld")
Will alert helloworld ten times, with an interval of 5 seconds each time

2.

Copy code The code is as follows:

Write a function stringconcat, which requires
var result1 = stringconcat("a", "b") result1 = "a b"
var stringconcatWithPrefix = stringconcat.prefix("hellworld");
var result2 = stringconcatWithPrefix("a", "b") result2 = "hellworld a b"

Snack solution

These two questions are all about closures. Without further ado, let’s go straight to the code.

Copy code The code is as follows:

/**
*The first question
* @param func
* @param times
* @param wait
* @returns {repeatImpl}
​*/
function repeat (func, times, wait) {
//Anonymous functions are not used to facilitate debugging
Function repeatImpl(){
      var handle,
​​​​​​ _arguments = arguments,
             i = 0;
         handle = setInterval(function(){
             i = i 1;
//Cancel the timer when the specified number of times is reached
                if(i === times){
Clearinterval (handle);
                  return;
            }
                func.apply(null, _arguments);
},wait);
}
Return repeatImpl;
}
//Test case
var repeatFun = repeat(alert, 4, 3000);
repeatFun("hellworld");
/**
*The second question
* @returns {string}
​*/
function stringconcat(){
var result = [];
Stringconcat.merge.call(null, result, arguments);
Return result.join(" ");
}
stringconcat.prefix = function(){
var _arguments = [],
​​​​ _this = this;
_this.merge.call(null, _arguments, arguments);
Return function(){
        var _args = _arguments.slice(0);
​​​​ _this.merge.call(null, _args, arguments);
          return _this.apply(null, _args);
};
};
stringconcat.merge = function(array, arrayLike){
var i = 0;
for(i = 0; i < arrayLike.length; i ){
         array.push(arrayLike[i]);
}
}
//Test case
var result1 = stringconcat("a", "b"); //result1 = "a b"
var result3 = stringconcat("c", "d"); //result1 = "a b"
var stringconcatWithPrefix = stringconcat.prefix("hellworld");
var stringconcatWithPrefix1 = stringconcat.prefix("hellworld1");
var result2 = stringconcatWithPrefix("a", "b"); //result2 = "hellworld a b"
var result4 = stringconcatWithPrefix1("c", "d"); //result2 = "hellworld a b"
alert(result1);
alert(result2);
alert(result3);
alert(result4);
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template