분류 전체보기
-
[Javascript] 이터레이터, 제너레이터프로그래밍 언어/JavaScript 2024. 1. 19. 18:32
이터레이션 프로토콜 일종의 약속인데 이 약속을 잘 지키는 데이터 컬렉션(객체, 배열 ...)은 for...of문으로 순회할 수 있고 spread문법을 적용할 수도 있다. 종류로 이터러블 프로토콜과 이터레이터 프로토콜이 있다. 이터러블 프로토콜 이터러블 프로토콜을 준수한 객체를 이터러블이라고 한며 이러한 이터러블은 Symbol.iterator 메소드(이터레이터를 반환)를 구현하거나 프로토타입 체인에 의해 상속한 객체를 말한다. 이러한 이터러블은 for...of문과 spread 연산자의 대상으로 사용 가능하다. - 배열은 Symbol.iterator 메소드를 소유하고 있고 일반 객체는 소유하고 있지 않다. 하지만 객체도 이터러블 프로토콜을 준수한다면 이터러블이 되며 이러한 방법으로 커스텀 이터러블이 있다. ..
-
[Javascript] 메모리 힙 & 콜 스택프로그래밍 언어/JavaScript 2024. 1. 19. 13:03
힙(heap)과 콜스택(call stack)이란? 자바스크립트 엔진이 코드를 읽고 실행하는 과정에서 중요한 두 단계로 정보를 저장하는 것, 코드를 트래킹하는 작업 두가지가 있다. 이때 정보를 저장하는 공간(메모리)가 메모리 힙이고, 코드를 트래킹하는 영역이 콜스택이다. 아래 코드를 통해 콜스택과 힙이 어떤식으로 작동하는지 알아보자 const a = 10 const b = 35 const arr = [] function func() { const c = a + b; // 스코프 체인에 따라 GEC의 VO 참조 obj = { d: c } return obj } const o = func() 1. 먼저 원시 값과 참조 값으로 구분되어서 각각 콜스택과 힙에 위치한다. 2. func() 가 실행되면서 새로운 컨텍..
-
[Javascript] prototype, class프로그래밍 언어/JavaScript 2024. 1. 18. 21:30
개요 프로토타입 클래스 언제 사용하는가? 문득 인벤이나 오피지지 같은 사이트를 만들고 싶다는 생각을 했을 때 게임 캐릭터에 대한 정보를 자료형으로 정리를 해야한다 그 때 자바스크립트 오브젝트 형식으로 정리를 하려고 할 때 하나하나 만드는 하드 코딩은 너무 비효율적이다. 그렇기에 비슷한 형식의 객체를 많이 많들어야 할 때 사용할 수있는 문법이 클래스이고 자바스크립트가 클래스 문법이 없었을 때는 어떻게 만들 수 있었는지 또한 프로토타입은 어떠한 영향을 끼치는지에 대해서 알아보자 프로토 타입 (Prototype) 자바스크립트는 프로토타입 기반의 객체지향 프로그래밍 언어이다. 이때 프로토 타입은 객체를 생성하기 위한 방법으로 객체를 생성하기 전 클래스를 정의해서 이를 통해서 인스턴스(객체)를 생성하는 방식과 다..
-
[Javascript] 메모리 할당프로그래밍 언어/JavaScript 2024. 1. 18. 18:39
메모리 할당 Javascript는 객체가 생성되었을 때 자동으로 메모리를 할당하고 필요하지 않으면 자동으로 해제(가비지 컬렉션)하는 자동 메모리 관리가 가능하다. 이러한 메모리 관리는 잠재적 혼란의 원인이 되기도 한다. 메모리 생존주기 필요할 때 할당 할당된 메모리 사용 (읽기, 쓰기) 필요 없으면 해제 자바스크립트는 일련의 과정이 암묵적으로 작동한다.(고수준 언어) 필요없을 때는 언제인가? 저수준의 언어(C언어)에서는 개발자가 직접 결정하고 해제하는 방식이지만 자바스크립트는 자동으로 하기에 필요없어진 시기를 알아내기 어렵다. 그렇기에 자동 메모리 관리 방법을 사용하는 가비지 콜렉터를 사용하여 메모리를 관리하는 것이 궁극적인 방법은 아니다 메모리의 필요 유무는 비결정적 문제이기 때문이다.