30 Day Vanilla JS Coding Challenge Study - 5주차
JavaScript KONAMI CODE!
Vanilla JavaScript Slide In on Scroll
JavaScript Fundamentals: Reference VS Copy
12. JavaScript KONAMI CODE! 영상: https://youtu.be/_A5eVOIqGLU
키 입력 감지
1 2 3 4 5 6 7 8 9 10 11 12 13 14 const pressed = [];const secretCode = 'wesbos' ;window .addEventListener ('keyup' , (e ) => { pressed.push (e.key ); pressed.splice (-secretCode.length - 1 , pressed.length - secretCode.length ); if (pressed.join ('' ).includes (secretCode)) { console .log ('DING DING!' ); cornify_add (); } });
영상: https://youtu.be/uzRsENVD3W8
스크롤 이벤트 활용하기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 function debounce (func, wait = 20 , immediate = true ) { var timeout; return function ( ) { var context = this ; var args = arguments ; var later = function ( ) { timeout = null ; if (!immediate) func.apply (context, args); }; var callNow = immediate && !timeout; clearTimeout (timeout); timeout = setTimeout (later, wait); if (callNow) func.apply (context, args); }; }; function checkSlide ( ) { sliderImages.forEach (sliderImage => { const slideInAt = (window .scrollY + window .innerHeight ) - sliderImage.height / 2 ; const imageBottom = sliderImage.offsetTop + sliderImage.height ; const isHalfShown = slideInAt > sliderImage.offsetTop ; const isNotScrolledPast = window .scrollY < imageBottom; if (isHalfShown && isNotScrolledPast) { sliderImage.classList .add ('active' ); } else { sliderImage.classList .remove ('active' ); } }); } window .addEventListener ('scroll' , debounce (checkSlide));
14. JavaScript Fundamentals: Reference VS Copy 영상: https://youtu.be/YnfwDQ5XYF4
JavaScript의 객체와 배열 그리고 참조 vs 복사
배열 1 2 3 4 5 6 7 8 9 10 11 12 13 14 const players = ['Wes' , 'Sarah' , 'Ryan' , 'Poppy' ];const team = players;team[3 ] = 'Lux' ; const team2 = players.slice ();const team3 = [].concat (players);const team4 = [...players];const team5 = Array .from (players);
객체 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 const person = { name : 'Wes Bos' , age : 80 }; const captain = person;captain.number = 99 ; const cap2 = Object .assign ({}, person, { number : 99 , age : 12 });const cap3 = {...person};
객체 (1 depth 초과) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 const wes = { name : 'Wes' , age : 100 , social : { twitter : '@wesbos' , facebook : 'wesbos.developer' } }; const dev = Object .assign ({}, wes);dev.social .twitter = '@coolman' ; const dev2 = JSON .parse (JSON .stringify (wes));