hw12.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // makeProfileTimer =====================================================================================
  2. function makeProfileTimer(){
  3. const start = performance.now()
  4. return () => {
  5. const finish = performance.now()
  6. return finish - start
  7. }
  8. }
  9. const timer = makeProfileTimer()
  10. console.log('Замеряем время работы этого console.log')
  11. console.log(`Время работы console.log = ${timer()} мкс`)
  12. // makeSaver =====================================================================================
  13. function makeSaver (func) {
  14. let funcResult
  15. let stopFunc = true
  16. return () => {
  17. if (stopFunc) {
  18. stopFunc = false
  19. funcResult = func()
  20. }
  21. console.log(funcResult)
  22. return funcResult
  23. }
  24. }
  25. const saver = makeSaver(Math.random)
  26. const value1 = saver()
  27. const value2 = saver()
  28. console.log(value1 === value2)
  29. const saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random()*6)])
  30. const value3 = saver2()
  31. const value4 = saver2()
  32. console.log(value3 === value4)
  33. const namePrompt = prompt.bind(window, 'Как тебя зовут?')
  34. const nameSaver = makeSaver(namePrompt)
  35. alert(`Привет! Prompt еще не было!`)
  36. alert(`Привет ${nameSaver()}. Только что запустился prompt, первый и последний раз`)
  37. alert(`Слушай, ${nameSaver()}, го пить пиво. Ведь prompt был только один раз`)
  38. // myBind =====================================================================================
  39. function myBind (func, funcThis, arr) {
  40. let result
  41. return function (...passParam) {
  42. for (let i =0; i < passParam.length; i ++) {
  43. for (let j = 0; j < arr.length; j ++) {
  44. if (arr[j] === undefined){
  45. arr[j] = passParam[i]
  46. passParam.shift()
  47. }
  48. }
  49. }
  50. result = func.call(funcThis,...arr)
  51. return console.log(result)
  52. }
  53. }
  54. const pow5 = myBind(Math.pow, Math, [, 5])
  55. pow5(2)
  56. const cube = myBind(Math.pow, Math, [, 3])
  57. cube(3)
  58. const chessMin = myBind(Math.min, Math, [, 4, , 5,, 8,, 9])
  59. chessMin(-1,-5,3,15)
  60. const zeroPrompt = myBind(prompt, window, [undefined, "0"])
  61. const someNumber = zeroPrompt("Введите число")
  62. const bindedJoiner = myBind((...params) => params.join(''), null, [, 'b', , , 'e', 'f'])
  63. bindedJoiner('a','c','d') === 'abcdef'
  64. bindedJoiner('1','2','3') === '1b23ef'
  65. // checkResult =====================================================================================
  66. function checkResult(original, validator){
  67. function wrapper(...params){
  68. const originalResult = original.call(this,...params)
  69. const checkValid = validator(originalResult)
  70. if (checkValid === true){
  71. return originalResult
  72. }
  73. else {
  74. return wrapper(...params)
  75. }
  76. }
  77. return wrapper
  78. }
  79. function me () {
  80. let meObj = {}
  81. meObj.name = prompt('Введите имя')
  82. meObj.surname = prompt('Введите фамилию')
  83. meObj.fatherName = prompt('Введите отчество')
  84. return meObj
  85. }
  86. const randomHigh = checkResult(Math.random, x => x>=0.5 && x<=1)
  87. const alwaysSayYes = checkResult(confirm, x => x === true)
  88. const respectMe = checkResult(me, x => Object.values(x).includes('') ? false : true)