main.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // /////////////////////////makeProfileTimer
  2. function makeProfileTimer() {
  3. let startTime = performance.now();
  4. return function () {
  5. return performance.now() - startTime;
  6. }
  7. };
  8. const timer = makeProfileTimer();
  9. alert('Замеряем время работы этого alert');
  10. alert(`Time for this alert running is: ${timer()}`);
  11. // ////////////////////// makeSaver
  12. function makeSaver(func) {
  13. let run1 = true;
  14. let saveResult;
  15. return function () {
  16. if (run1) {
  17. saveResult = func();
  18. run1 = false;
  19. console.log(`Result is saved: ${saveResult}`);
  20. }
  21. return saveResult;
  22. }
  23. }
  24. var saver = makeSaver(Math.random);
  25. var value1 = saver();
  26. var value2 = saver();
  27. value1 === value2;
  28. var saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random() * 6)]);
  29. var value3 = saver2();
  30. var value4 = saver2();
  31. value3 === value4;
  32. // //////////////////////////Final Countdown
  33. function finalCountdown() {
  34. for (let i = 0; i < 5; i++) {
  35. setTimeout(() => console.log(5 - i), 1000 * i)
  36. }
  37. setTimeout(() => console.log("поехали !"), 5000)
  38. }
  39. // ////////////////////////////myBind
  40. function myBind(func, tempThis, tempArray) {
  41. return function (...arguments) {
  42. let i = 0;
  43. let newTempArray = tempArray.map((value) => (value === undefined ? arguments[i++] : value));
  44. return func.apply(tempThis, newTempArray);
  45. };
  46. }