function add(x, y){
return x + y;
}
add(3, 5)
function add(a,b) {
return a + b;
}
add(10,20)
function add(a = 100, b = 200) {
return a + b;
}
function add({ a = 100, b = 200 }) {
console.log(a+b);
}
add({b: 300}); // 400
let k = 10;
function viewk() {
console.log(k);
}
function changek() {
k += 10;
console.log(k)
}
viewk() -> 10
changek() ->20
let yy = 100
if (true) {
let yy = 10
colsole.log(yy)
}
console.log(yy)
출력
10
100
let k = 10;
function viewk() {
console.log(k);
}
function changek(){
let k = 20;
console.log(k)
}
viewk() //10
changek() //20
viewk() //10
자바스크립트 엔진은 생성단계에서 한번 선언문을 쭉 훑고 선언문들을 환경 레코드(enviroment recod) 에 기록해둠
변수 호이스팅(variable Hoisting) : var, let, const
var키워드로 선언한 것은 undefined로 값을 초기화 해둔다.
let, const로 하면 식별자는 기록해두지만, 값을 초기화하지 않는다.(일시적 사각지대)
함수 표현식에서 변수가 기록되고, 그 값은
var의 경우 undefined, let,const의 경우 초기화가 이루어지지 않은 상황에서
함수를 호출하면, 데이터 타입은 undefined는 호출될 수 없기 때문에 타입에러가 발생
let 이나 const는 아직 환경레코드에 기록된 값이 없기 때문에 Reference error가 발생
함수 호이스팅(function hoistion) : 함수를 선언과 동시에 완성된 함수객체를 생성하여 환경레코드에 기록해 둔다.선언전에도 함수를 사용할 수 있다.
console.log(add1(10, 20)); // 30
console.log(add2(10, 20)); // 30
console.log(mul1) // undefined
console.log(mul1(10, 20)); // not a function
console.log(mul2); // Cannot access 'mul2' before initialization
console.log(mul2(10, 20)); // 위와 같은 애러
console.log(mul3) // mul3 is not defined, 호이스팅이 안되었기 때문
function add1(x, y) {
return x + y;
}
function add2(x, y) {
return x + y;
}
var mul1 = function (a, b) {
return a * b;
}
let mul2 = function (a, b) {
return a * b;
}
function add2(x, y) {
return x + y;
}
💡 함수 선언문으로 선언된 함수는 함수 호이스팅이 발생한다. 런타임 이전에 함수 객체가 생성이 되므로 함수가 정의되기 전에 함수를 호출할 수 있다. 미리 함수 호출해도 가능, 클로저 상황에 많이 사용.
console.log(mul(10, 20));
let mul = function (a, b) {
return a * b;
}
💡 함수 표현식으로 선언된 함수는 변수 호이스팅이 발생하며, undefined로 초기화된다. 그러므로 함수 선언문으로 선언한 함수는 함수가 정의되기 전에 함수를 호출할 수 없다.
console.log(add1(10, 20));
console.log(add2(10, 20));
// 호이스팅 되어서 위로 올라갑니다!
function add1(x, y) {
return x + y;
}
console.log(mul(10, 20));
let mul = function (a, b) {
return a * b;
}
function add2(x, y) {
return x + y;
}
function 제곱(x) {
function 승수(y) {
return y ** x
}
return 승수
}
재곱(2)(10) // 출력 100
let 제곱2 = 제곱(2)
제곱2(10)
let 제곱3 = 제곱(3)
제곱3(10)
// 위랑 동일
function 제곱(x) {
return function(y) {
return y ** x
}
}
<비교해보기>
const x = 100;
function a() {
const x = 1;
}
function b() {
console.log(x);
}
a(); // 100
b(); // 100 //바라보는게 x 선언된 값임
// 비교대상
const xx = 100;
function a() {
const xx = 1;
function b() {
console.log(xx);
}
b();
}
a(); //출력 1
function User(name) {
this.name = name;
console.log(this.name);
}
let user = User("jina");
user.name //출력: undefined
function User(name) {
this.name = name;
console.log(this.name);
}
let user = new User("Jina");
user.name // jina 출력
function add(x, y) {
return x + y;
}
function mul(x, y) {
return x * y;
}
function cal(a, b){
return a(10, 10) + b(10, 10);
}
cal(add, mul);
let func = function(arg1,arg2, ...argN) {
return expression;
}
let func = (arg1,arg2, ...argN) => expression
(function () {
var a = 1;
var b = 2;
return a + b;
}());
function test() {
x= 10
}
console.log(x) // 10이 출력됨
function test() {
'use static'
k= 10
}
console.log(k) // k is not defined
점점 더 어려워지는 자바스크립트😭
그래도 한문제 풀릴때마다 즐거워서 놓을 수 없다 🤣