123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- // makeProfileTimer ==============================================================================
- makeProfileTimer = () => {
- const starTime = performance.now()
- console.log('START: ' + starTime + ' Ms')
- return () => {
- const finishTime = performance.now()
- console.log('FINISH: ' + finishTime + ' Ms')
- const result = finishTime - starTime
- return console.log('RESULT: ' + result + ' Ms')
- }
- }
- const timer = makeProfileTimer()
- console.log('Замеряем время работы этого console.log');
- console.log(timer())
- // makeSaver ==============================================================================
- makeSaver = (arg) => {
- let result
- let stopFn = true
- return () => {
- if(stopFn){
- stopFn = false
- result = arg()
- }
- console.log(result)
- return result
- }
- }
- let saver = makeSaver(Math.random)
- let value1 = saver()
- let value2 = saver()
- console.log(value1 === value2)
- let saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random()*6)])
- let value3 = saver2()
- let value4 = saver2()
- console.log(value3 === value4)
- let namePrompt = prompt.bind(window, 'Как тебя зовут?')
- let nameSaver = makeSaver(namePrompt)
- alert(`Привет! Prompt еще не было!`)
- alert(`Привет ${nameSaver()}. Только что запустился prompt, первый и последний раз`)
- alert(`Слушай, ${nameSaver()}, го пить пиво. Ведь prompt был только один раз`)
- // Final Countdown ==============================================================================
- (finalCountdown = (counterVal) => {
- countingFn = () => {
- counterVal ? console.log(counterVal--) : clearInterval(interval)
- }
- const interval = setInterval(countingFn,1000)
- })(5);
- // &&
- const finalCountdown = (counterVal) => {
- let result
- return (() => {
- for(let i = counterVal; i >= 0; i--){
- result=counterVal
- if(result == 0) return
- }
- console.log(result--)
- setTimeout(finalCountdown,1000,result)
-
- })()
- }
- finalCountdown(5)
- // myBind ==============================================================================
- function myBind( fn, isThis, arr ) {
- let result
- return function (...passParam) {
- for(let i = 0; i < passParam.length; i ++){
- for(let j = 0; j < arr.length; j ++){
-
- if(arr[j] === undefined){
- arr[j] = passParam[i]
- passParam.shift()
- }
- }
- }
- result = fn.call(isThis,...arr)
- return console.log(result)
- }
- }
- var pow5 = myBind(Math.pow, Math, [undefined, 5])
- pow5(2)
- var cube = myBind(Math.pow, Math, [undefined, 3])
- cube(3)
- var chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5,undefined, 8,undefined, 9])
- chessMin(-1,-5,3,15)
- var zeroPrompt = myBind(prompt, window, [undefined, "0"])
- var someNumber = zeroPrompt("Введите число")
- myBind( (...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'] ) ('a','c','d') === 'abcdef'
- // ============================================================================================================================================================
|