기본 콘텐츠로 건너뛰기

4월, 2019의 게시물 표시

6.11 JS Currying and Particials class note and task solving

args.length = a number of args passed by curried function func.legnth = original function's args' number concate next function's argument till it doesn't need any more currring when binded function needs arguments, do not write user.longin(true).bind(user) just like I tried lol but user.login.bind(user, true) //function.bind(context, argument) in that order in number2 way, each function binded are called PARTICIAL functions of user.login

JS 6.9 task 4 what the fuck is this / Throttle decorator

<HTML>   <body>   </body>    <script>    function f(a) {      console.log(a)    };    // f1000 passes calls to f at maximum once per 1000 ms    let f1000 = throttle(f, 1000); function throttle(update, ms){     let isThrottled = false,     savedArgs,     savedThis; //context can be saved in let     function wrapper(){       if(isThrottled) { //if throttled is true, save latest call         savedArgs = arguments;         savedThis = this;         return;       }       update.apply(this,arguments);       //runs and book the next call       isThrottled = true;       setTimeout(function() {         //initiate the flag object and let wrapper runs again         isThrottled = false;         if(savedArgs) {           wrapper.apply(savedThis, savedArgs);           //calling the whole function wrapper makes the latest call's infos to be saved           savedArgs = savedThis = null;         }       }, ms);     }     return wrapper;

JS 6.9 task3 Debounce decorator

<HTML>   <body>   </body>    <script>    let f = debounce(alert, 1000); //setting default value of time    function debounce(f, ms) {      let isCooldown = false;      return function() {        if (isCooldown) return; //ignore the call and end the function        //if its false, then call the f(=alert)        f.apply(this, arguments);        isCooldown = true;//flag is on to ignore future calls       setTimeout(() => isCooldown = false, ms);        //set timeout to turn flag to be false later      };    } f(1); // runs immediately f(2); // ignored setTimeout( () => f(3), 100);  // ignored ( only 100 ms passed ) setTimeout( () => f(4), 1100); // runs setTimeout( () => f(5), 1500); // ignored (less than 1000 ms from the last run)    </script> </HTML>

JS 6.9 task2 Delaying decorator

<HTML>   <body>   </body>    <script>    function f(x) {   alert(x); }   function delay(func, sec){     return function(){       setTimeout(()=>f.apply(this, arguments), sec);     };     //arrow function do not have this,     //f.apply takes context and arguments from delay, outter function     //that is why filling arguments like setTimeout(fun(args))     //always returned undefined   } // create wrappers let f1000 = delay(f, 1000); let f1500 = delay(f, 1500); f1000("test"); // shows "test" after 1000ms f1500("test"); // shows "test" after 1500ms    </script> </HTML>

JS 6.9 task1 Spy decorator

<HTML>   <body>   </body>    <script> function work(a,b){   alert(a + b); } function spy(func){   function wrapper (...args) {     //to make parameters to be each array by every call     wrapper.calls.push(args);     //this is call forwarding : wrapper passes everything it gets     //and return back its result     return func.apply(this, arguments);     //arguments is reserved word,     // means all argument the outter object has got   }//function definition can be anywhere in the block   wrapper.calls = []; //calls is an array   return wrapper; } work = spy(work); //now work has the body of wrapper //which has an array calls, and does push everytime it runs work(1, 2); // 3 work(4, 5); // 9 for (let args of work.calls) {   alert( 'call:' + args.join() ); // "call:1,2", "call:4,5" }    </script> </HTML> <!-- func.call(context, ...args) //spread operater(...) allows to pass ITERABLE (

JS 6.8 task1 Output every second

<HTML>   <body>   </body>    <script>    //inverval way    // function printNumbers(from, to){    //   let i = from;    //   let interval = setInterval(function(){    //     alert(i);    //     if(i < to) i++; // 0 ~ 4    //     else clearInterval(interval);    //   },1000);    // }    //settimeout way, solution, mostly by myself tho    function printNumbers(from, to){      let i = from;     setTimeout(function run(){        alert(i);        if(i < to){          setTimeout(run,1000);        }        i++;      },1000);    }    printNumbers(0,5);    </script>    <!--    setting a timeout conditionally, be careful,    putting setTimeout(run(), 1000); doesnt finish the setting    but runs the function actually    --> </HTML>