12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- //profiletimer
- function makeProfileTimer() {
- let start = performance.now ()
- function finishOfTime () {
- let finish = performance.now()
- return (finish - start) / 1000;
- }
- return finishOfTime;
- }
- var timer = makeProfileTimer()
- alert('Замеряем время работы этого alert');
- alert(timer())
- //makesaver
- function makeSaver (fn){
- let result;
- let somethingThatHelp = true ;
- return () => {
- if (somethingThatHelp){
- result = fn();
- }
- somethingThatHelp = false
- return result
- }
- }
-
- let nameSaver = makeSaver(() => prompt('введите имя'))
- alert ('имя еще не спрашивали')
- let name1 = nameSaver()
- alert ('just ask name')
- let name2 = nameSaver()
- alert ("tho times ask?")
- alert ("hello , " + nameSaver() + (nameSaver() === nameSaver() ? ". done" : 'shit happens'))
- //finalCountdouwn
- function finalCountdown() {
- let countdown = 5;
- function someFunction() {
- console.log(countdown--)
- }
- if (!countdown){
- clearTimeout(timer)
- }
- for (let i = 1; i <6; i++){
- let timer = setTimeout(someFunction , 1000*i)
- }
- }
-
- finalCountdown()
- //myBind
- function myBind (funcn, context, arr) {
- return (...params) => {
- let indexParam = 0;
- let newArray = arr.map((i) => (i === undefined ? params[indexParam++] : i))
- return func.call(context, ...newArray)
- }
- }
-
- var pow5 = myBind(Math.pow, Math, [undefined, 5])
- var cube = myBind(Math.pow, Math, [undefined, 3])
-
- alert(pow5(2)) // => 32, вызывает Math.pow(2,5), соотнесите с [undefined, 5]
- alert (cube(3)) // => 27
-
-
- var chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5,undefined, 8,undefined, 9])
- chessMin(-1,-5,3,15) // вызывает Math.min(-1, 4, -5, 5, 3, 8, 15, 9), результат -5
-
-
-
- var zeroPrompt = myBind(prompt, window, [undefined, "0"])
- var someNumber = zeroPrompt("Введите число") // вызывает prompt("Введите число","0")
-
- console.log(myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a','c','d') === 'abcdef')
-
|