|
@@ -0,0 +1,80 @@
|
|
|
+//makeProfileTimer
|
|
|
+function makeProfileTimer() {
|
|
|
+ let prev = performance.now();
|
|
|
+
|
|
|
+ return function () {
|
|
|
+ let next = performance.now();
|
|
|
+ return ("Время выполнения: " + (next - prev));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//makeSaver
|
|
|
+function makeSaver(el) {
|
|
|
+ let result;
|
|
|
+ let doFunc = true;
|
|
|
+
|
|
|
+ return function () {
|
|
|
+ if (doFunc) {
|
|
|
+ result = el();
|
|
|
+ doFunc = false;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+var saver = makeSaver(Math.random);
|
|
|
+var value1 = saver();
|
|
|
+var value2 = saver();
|
|
|
+value1 === value2 // true
|
|
|
+
|
|
|
+var saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random() * 6)])
|
|
|
+var value3 = saver2()
|
|
|
+var value4 = saver2()
|
|
|
+
|
|
|
+value3 === value4 // тоже true
|
|
|
+
|
|
|
+
|
|
|
+//Final Countdown
|
|
|
+function finalCountdown(lastCountNumber) { //параметр число с которого начинается обратный отсчёт
|
|
|
+ if (lastCountNumber >= 0) {
|
|
|
+ setTimeout((function () {
|
|
|
+ lastCountNumber === 0 ? console.log("поехали!") : console.log(lastCountNumber);
|
|
|
+
|
|
|
+ finalCountdown(lastCountNumber - 1);
|
|
|
+ }), 1000)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//myBind
|
|
|
+function myBind(func, context, ...args) {
|
|
|
+ let argsCopy = args;
|
|
|
+ argsCopy = argsCopy.flat();
|
|
|
+
|
|
|
+ return function (...args2) {
|
|
|
+ let j = 0;
|
|
|
+
|
|
|
+ for (let i = 0; i < argsCopy.length; i++) {
|
|
|
+ if (argsCopy[i] === undefined) {
|
|
|
+ argsCopy[i] = args2[j]
|
|
|
+ j++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return func.call(context, ...argsCopy)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+var pow5 = myBind(Math.pow, Math, [undefined, 5])
|
|
|
+var cube = myBind(Math.pow, Math, [undefined, 3])
|
|
|
+
|
|
|
+pow5(2) // => 32, вызывает Math.pow(2,5)
|
|
|
+cube(3) // => 27, вызывает Math.pow(3,3)
|
|
|
+
|
|
|
+
|
|
|
+var chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5, undefined, 8, undefined, 9])
|
|
|
+chessMin(-1, -5, 3, 15) // вызывает Math.min(-1, 4, -5, 5, 3, 8, 15, 9), результат -5
|
|
|
+
|
|
|
+var zeroPrompt = myBind(prompt, window, [undefined, "0"])
|
|
|
+var someNumber = zeroPrompt("Введите число") // вызывает prompt("Введите число","0")
|
|
|
+
|
|
|
+myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a', 'c', 'd') === 'abcdef' //true
|
|
|
+
|