Hi, I’m Kang Byeong-hyeon.

..

[카카오 테크 캠퍼스 2기] 1단계 4주차 WIL - JavaScript 핵심




1. 연산자

산술 연산자(Arithmetic Operator)

console.log(1 + 2); // 덧셈, 3
console.log(5 - 7); // 뺄셈, 2
console.log(3 * 4); // 곱, 12
console.log(10 / 2); // 나눗셈, 5
console.log(7 % 5); // 나머지, 2

할당 연산자(Assignment Operator)

a = a + 1; // a += 1
a = a - 1; // a -= 1
a = a * 1; // a *= 1
a = a / 1; // a /= 1

비교 연산자(Comparison Operator)

const a = 1
const b = 1
console.log(a === b) // true, 일치 연산자(데이터 타입 + 값)

const isEqual(x, y) {
	return x === y
}

console.log(isEqual(1, 1)) // true
console.log(isEqual(2, '2')) // false

const a = 1
const b = 3
console.log(a !== b) // true

console.log(a < b) // true
console.log(a > b) // false

const a = 13
const b = 13
console.log(a <= b) // true
console.log(a >= b) // true

논리 연산자(Logical Operator)

const a = 1 === 1;
const b = "AB" === "AB";
const c = false;

console.log(a); // true
console.log(b); // true
console.log(c); // false

console.log("&& ", a && c); // false
console.log("||: ", a || b || c); // true

삼항 연산자(Ternary Operator)

const a = 1 < 2; // true

if (a) {
  console.log(""); // ✅
} else {
  console.log("거짓");
}

console.log(a ? "" : "거짓"); // '참'




2. 데이터 타입

형변환(Type conversion)

const a = 1;
const b = "1";

console.log(a === b); // false
console.log(a == b); // true

Truthy, Falsy

if (true) {
  console.log(123);
}

if ("false") {
  // 문자 => Truthy
  console.log(123);
}




3. 함수

arguments 객체

function sum() {
  console.log(arguments);
  return;
}

console.log(sum(7, 3));

화살표 함수(arrow function)

const doubleArrow = (x) => {
  return x * 2;
};

const doubleArrow = (x) => x * 2;

const doubleArrow = x({ name: "byeonghyeon" });

즉시실행함수(IIFE)

const a = 7(function () {
  console.log(a * 2);
})();

호이스팅(Hoisting)

함수 선언부가 유효범위 최상단으로 끌어 올려지는 것 처럼 보이는 현상을 의미한다.

const a = 7;

double(); // 14

function double() {
  console.log(a * 2);
}




4. 클래스

생성자 함수(prototype)

// 생성자 함수
function User(first, last) {
  this.firstName = first;
  this.lastName = last;
}

// prototype 객체를 통한 메모리 관리
User.prototype.getFullName = function () {
  return `${this.firstName} ${this.lastName}`;
};

// 인스턴스 생성
const heropy = new User("Heropy", "Park");
const amy = new User("Amy", " Clarke");
const neo = new User("Neo", "Smith");

ES6 클래스 패턴

class User {
  // 생성자 함수
  constructor(first, last) {
    this.firstName = first;
    this.lastName = last;
  }

  // prototype method 정의
  getFullName() {
    return `${this.first} ${this.lastName}`;
  }
}

상속(extends)

class Vehicle {
  constructor(name, wheel) {
    this.name = name;
    this.wheel = wheel;
  }
}

class Bicycle extends Vehicle {
  constructor(name, wheel) {
    super(name, wheel);
  }
}

class Car extends Vehicle {
  constructor(name, wheel, license) {
    super(name, wheel);
    this.license = license;
  }
}




5. 데이터

데이터 불변성(immutable)

원시 데이터

참조 데이터

복사

얕은 복사(shallow copy)

새로운 참조 데이터가 새로운 메모리 주소에 할당한다.

깊은 복사(deep copy)

데이터 내부에 참조 데이터가 있을 경우 해당 데이터는 같은 메모리를 참조하기 때문에 깊은 복사가 필요하다.

const user = {
  name: "Heropy",
  age: 85,
  emails: ["thesecon@gmail.com"],
};

const copyUser = { ...user };

console.log(user === copyUser); // false
user.age = 22;

user.emails.push("neo@zillinks.com");
console.log(user.emails === copyUser.emails); // ⚠️ true
import _ from "lodash";

const user = {
  name: "Heropy",
  age: 85,
  emails: ["thesecon@gmail.com"],
};

const copyUser = _.cloneDeep(user);

console.log(user === copyUser); // false
user.age = 22;

console.log("user", user); // age: 22
console.log("copyUser", copyUser); // age: 85

user.emails.push("neo@zillinks.com");
console.log(user.emails === copyUser.emails); // false