'use strict'; //Замыкания const makeProfileTimer = () => { const start = performance.now(); return () => performance.now() - start; }; const timer = makeProfileTimer(); alert('Замеряем время работы этого alert'); alert(timer()); const makeSaver = (value) => () => value; const saver = makeSaver(Math.random); const value1 = saver(); const value2 = saver(); console.log(value1 === value2, 'value1 === value2'); const saver2 = makeSaver( () => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random() * 6)] ); const value3 = saver2(); const value4 = saver2(); console.log(value3 === value4, 'value3 === value4'); // тоже должно быть true (function selfCountDown(n) { if (n === 0) return; setTimeout(() => { selfCountDown(n - 1); }, 1000); })(5); const myBind = function (fnBind, context, settings) { return function (...params) { let i = 0; const array = settings.map((settingsItem) => { if (settingsItem === undefined) { const paramsItem = params[i]; i += 1; return paramsItem; } else { return settingsItem; } }); return fnBind.apply(context, array); }; }; const pow5 = myBind(Math.pow, Math, [undefined, 5]); const cube = myBind(Math.pow, Math, [undefined, 3]); console.log(pow5(2)); // => 32, вызывает Math.pow(2,5), соотнесите с [undefined, 5] console.log(cube(3)); // => 27 const chessMin = myBind(Math.min, Math, [ undefined, 4, undefined, 5, undefined, 8, undefined, 9, ]); console.log(chessMin(-1, -5, 3, 15)); const zeroPrompt = myBind(prompt, window, [undefined, '0']); const someNumber = zeroPrompt('Введите число myBind'); // вызывает prompt("Введите число","0")\ console.log( myBind((...params) => params.join(''), null, [ undefined, 'b', undefined, undefined, 'e', 'f', ])('a', 'c', 'd') === 'abcdef' );