Millet company JavaScript closed-pack face test
One, please define such a function
The code is as follows |
Copy Code |
function repeat (func, times, wait) { } This function can return a new function, such as using the var repeatedfun = repeat (alert, 10, 5000) Call this repeatedfun ("Hellworld") Alert 10 times HelloWorld, each interval of 5 seconds |
Two, write a function stringconcat, request can
The code is as follows |
Copy Code |
var result1 = Stringconcat ("A", "B") Result1 = "A+b" var stringconcatwithprefix = Stringconcat.prefix ("Hellworld"); var result2 = Stringconcatwithprefix ("A", "B") result2 = "Hellworld+a+b" |
Solution
These two questions, the test is closure, nonsense not much said, directly on the code.
The code is as follows |
Copy Code |
/** * First question * @param func * @param times * @param wait * @returns {Repeatimpl} */ function repeat (func, times, wait) { No anonymous function is necessary to facilitate debugging function Repeatimpl () { var handle, _arguments = arguments, i = 0; Handle = SetInterval (function () { i = i + 1; Cancel timer at specified number of times if (i = = times) { Clearinterval (handle); Return } Func.apply (null, _arguments); },wait); } return Repeatimpl; } Test Cases var repeatfun = repeat (Alert, 4, 3000); Repeatfun ("Hellworld"); /** * 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 Cases 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); |