목록Javascript (17)
솔미는 성장중
메소드(객체 내 함수) this -> 객체 함수 this -> window object constructor function this -> 빈 객체를 가리킴 (여기다가 속성을 넣는거) 화살표 함수 this -> 상위 스코프 this 일반 함수의 this : 호출 위치에서 정의 : 아래 예제에서 this → PPAP로 바꿔주어도 결과가 동일 : 즉! this = getFullName이라는 속성이 들어있는 객체 데이터 const PPAP = { front: 'PenPineapple', end: 'ApplePen', getFullName: function() { return `${this.front} ${this.end}` } } console.log(PPAP.getFullName()) //PenPineapp..
기본 개념 setTimeout (실행할 내용, 시간) '시간' 후에 1번 실행 실행할 내용 적는 방법 () => {내용} 함수명 clearTimeout (대상) 타이머를 해제한다. setInterval (실행할 내용, 시간) '시간'마다 실행 clearInterval (대상) setInterval로 실행하던 것 해제 예제 [index.html] Click Me [main.js] const HelloWorld = () => { console.log('HelloWorld!') } const timeout = setInterval(HelloWorld,1000) const h1El = document.querySelector('h1') h1El.addEventListener('click',()=>{ consol..
하나의 함수에서 함수 자기 자신을 내부에서 다시 호출하는 것 기본적으로 무한 동작하기 때문에 필요할 때 멈춰줄 수 있게 만들어줘야 한다. 예시) 재귀 함수를 통해 최상위 포식자 찾기 const animalA = {name: 'A', predator: null} const animalB = {name: 'B', predator: animalA} const animalC = {name: 'C', predator: animalB} const getFinalPredator = animal => { if(animal.predator){ return getFinalPredator(animal.predator) } return animal.name //A } console.log(getFinalPredator(ani..
즉시 실행 함수 (IIFE) Immediately-Invoked Function Expression 별도의 호출 없이 바로 실행되길 바랄 때 사용 주로 변수를 전역으로 선언하는 것을 피하기 위해 사용 내부에서 선언한 변수는 외부에서 접근 불가 사용 패턴 1번째 방식 (화살표 함수) (F) ( ) (() => {})() 2번째 방식 (일반 함수) (F) ( ) (function() {})() 3번째 방식 (F( ) ) ((function() {})()) 4번째 방식 ! F ( ) !function() {}() 5번째 방식 + F ( ) +function() {}() const a = 5 const double = () => { console.log(a*2) } (() => {console.log(a*2)}..
함수 선언하는 방식 1. 함수 선언문 function A(매개변수) { } 2. 함수 표현식 const A = function (매개변수) { } ------------------------------------------------ 3. 화살표 함수 const A = (매개변수) => { } 그 중 화살표 함수에 대해 알아보도록 하겠습니다! 화살표 함수의 패턴 매개변수가 1개라면 매개변수를 감싼 소괄호를 없앨 수 있다. (0개, 2개, 3개, ... 모두 다 생략하면 안 됨) const A = x => {} 함수 로직이 return키워드로 시작한다면 대괄호와 함께 생략해줄 수 있다. const B = ..