hw9.1.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // makeProfileTimer ==============================================================================
  2. makeProfileTimer = () => {
  3. const starTime = performance.now()
  4. console.log('START: ' + starTime + ' Ms')
  5. return () => {
  6. const finishTime = performance.now()
  7. console.log('FINISH: ' + finishTime + ' Ms')
  8. const result = finishTime - starTime
  9. return console.log('RESULT: ' + result + ' Ms')
  10. }
  11. }
  12. const timer = makeProfileTimer()
  13. console.log('Замеряем время работы этого console.log');
  14. console.log(timer())
  15. // makeSaver ==============================================================================
  16. makeSaver = (arg) => {
  17. let result
  18. let stopFn = true
  19. return () => {
  20. if(stopFn){
  21. stopFn = false
  22. result = arg()
  23. }
  24. console.log(result)
  25. return result
  26. }
  27. }
  28. let saver = makeSaver(Math.random)
  29. let value1 = saver()
  30. let value2 = saver()
  31. console.log(value1 === value2)
  32. let saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random()*6)])
  33. let value3 = saver2()
  34. let value4 = saver2()
  35. console.log(value3 === value4)
  36. let namePrompt = prompt.bind(window, 'Как тебя зовут?')
  37. let nameSaver = makeSaver(namePrompt)
  38. alert(`Привет! Prompt еще не было!`)
  39. alert(`Привет ${nameSaver()}. Только что запустился prompt, первый и последний раз`)
  40. alert(`Слушай, ${nameSaver()}, го пить пиво. Ведь prompt был только один раз`)
  41. // Final Countdown ==============================================================================
  42. (finalCountdown = (counterVal) => {
  43. countingFn = () => {
  44. counterVal ? console.log(counterVal--) : clearInterval(interval)
  45. }
  46. const interval = setInterval(countingFn,1000)
  47. })(5);
  48. // &&
  49. const finalCountdown = (counterVal) => {
  50. let result
  51. return (() => {
  52. for(let i = counterVal; i >= 0; i--){
  53. result=counterVal
  54. if(result == 0) return
  55. }
  56. console.log(result--)
  57. setTimeout(finalCountdown,1000,result)
  58. })()
  59. }
  60. finalCountdown(5)
  61. // myBind ==============================================================================
  62. function myBind( fn, isThis, arr ) {
  63. let result
  64. return function (...passParam) {
  65. for(let i = 0; i < passParam.length; i ++){
  66. for(let j = 0; j < arr.length; j ++){
  67. if(arr[j] === undefined){
  68. arr[j] = passParam[i]
  69. passParam.shift()
  70. }
  71. }
  72. }
  73. result = fn.call(isThis,...arr)
  74. return console.log(result)
  75. }
  76. }
  77. var pow5 = myBind(Math.pow, Math, [undefined, 5])
  78. pow5(2)
  79. var cube = myBind(Math.pow, Math, [undefined, 3])
  80. cube(3)
  81. var chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5,undefined, 8,undefined, 9])
  82. chessMin(-1,-5,3,15)
  83. var zeroPrompt = myBind(prompt, window, [undefined, "0"])
  84. var someNumber = zeroPrompt("Введите число")
  85. myBind( (...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'] ) ('a','c','d') === 'abcdef'
  86. // ============================================================================================================================================================