123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Замыкание</title>
- </head>
- <body>
- <script>
-
- //---------makeProfileTimer
- /*function makeProfileTimer(){
- let time1=performance.now();
- return function(){
- let time2=performance.now();
- alert(time2-time1);
- }
- }
- function fib(n){
- if(n==1||n==2){
- return 1;
- }else{
- return fib(n-1)+fib(n-2);
- }
- }
-
- var timer=makeProfileTimer();
- alert(fib(10));
- timer();*/
- //-----------makeSaver
- function makeSaver(fn){
- let x=fn();
- return function(){
- return x;
- }
- }
- var saver = makeSaver(Math.random);
- var value1 = saver();
- var value2 = saver();
- console.log(value1+" "+value2);
- console.log(value1 === value2);
- var saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random()*6)]);
-
- var value3 = saver2();
- var 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
- /*(function count(){
- let x=5;
- let y=setInterval(function(){
- console.log(x);
- if(x==1){
- console.log('поехали');
- clearInterval(y);
- }
- x--;
- },1000)
- })();*/
- //------------myBind
- /*function myBind(fn,context,args){
- return function(){
- let args2=Object.assign(args,arguments);
- return fn.call(context,...args2);
- }
- }
- var pow5 = myBind(Math.pow, Math, [undefined, 5]);
- var cube = myBind(Math.pow, Math, [undefined, 3]);
- console.log(pow5(2)); //32
- console.log(cube(3)); //27
- var chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5,undefined, 8,undefined, 9]);
- console.log(chessMin(-1,-5,3,15)); //NaN ???
- var zeroPrompt = myBind(prompt, window, [undefined, "0"]);
- var someNumber = zeroPrompt("Введите число");
- console.log(someNumber);
- console.log(myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a','c','d') === 'abcdef');*/
- </script>
- </body>
- </html>
|