Function(3)
-
Javascript 스코프(Scope)
1. var (function-level scope) - 함수 안에서 선언된 변수는 함수 안에서만 유효하다. 외부는 X function varScope() { if (true) { var a = "var 스코프"; console.log(a); } console.log(a); } func(); console.log(a); // 에러발생 /* === 실행 결과 === var 스코프 var 스코프 Uncaught ReferenceError: a is not defined */ 2. let / const (block-level scope) - 코드 블록 {} 내부에서 선언된 변수는 블록안에서만 유효. 외부는 X function blockScope() { if (true) { let a = "block scope..
2023.05.16 -
6. 함수
def open_account(): print("새로운 계좌가 생성되었습니다.") open_account() ''' 새로운 계좌가 생성되었습니다. ''' def deposit(balance, money): print("입금이 완료되었습니다. 잔액은 {0} 원입니다.".format(balance + money)) return balance + money def withdraw(balance, money): # 출금 if balance >= money: # 잔액이 출금보다 많으면 print("출금이 완료되었습니다. 잔액은 {0} 원입니다.".format(balance - money)) return balance - money else: print("출금이 완료되지 않았습니다. 잔액은 {0} 원입니다.".fo..
2023.05.03 -
화살표 함수
일반함수 function add1(x, y) { return x+y; } 화살표 함수 const add1 = (x, y) ⇒ { return x+y; } const add2 = (x,y) ⇒ x+y; const add3 = (x,y) ⇒ (x + y); const not = x ⇒ !x; // 객체를 리턴하는 것은 소괄호가 필수다 const obj = (x, y) ⇒ ({x, y}) - 화살표 함수가 기존 function() {} 을 대체하는 건 아니다. (this 가 달라지기 때문) - 화살표 함수는 부모의 this 를 물려받는다. - 일반 함수는 부모의 this 와 나의 this 가 다르다 . 즉 자기만의 this 를 가진다.
2023.04.08