sangwoo_rhie 2023. 7. 11. 15:50

 

원시 타입 : 단일한 값을 담을 수 있는 타입 (숫자, 문자열, boolean, null, undefined)

변수선언시 변수에 값이 담기며, 다른 변수에 대입시 값(value)에 의한 복사가 일어난다.

const num = 2
const str = "자바스크립트"
const isFemail = true
const type = null

const str2 = str


Ex)
let myScore = 80
let yourScore = myScore
myScore = 100

console.log(myScore)  => 100
console.log(yourScore) => 80

 

객체 타입 : 연관된 데이터를 담을 수 있는 타입 (object, array, function)

변수 선언시 변수에는 메모리의 주소가 저장되고, 다른 변수에 대입 시 주소(reference)에 의한 복사가 일어난다.

얕은복사(Shallow copy)문제는 이같은 이유 때문에 발생한다.

const obj = {a: 1, b: 2}
const arr = [1, 2, 3, 4, 5, 6]
const func = () => {
  console.log('function call')
}

const arr2 = arr

Ex)
let order = [1, 2, 3, 4, 5]
let anotherOrder = order
anotherOrder[0] = 5

console.log(order)        => [5, 2, 3, 4, 5]
console.log(anotherOrder) =>  [5, 2, 3, 4, 5]

 

 

코드영역: 실행 프로그램 코드가 저장되는 영역, 즉 컴퓨터가 이해할 수 있는 바이너리 코드가 저장되는 영역. CPU가 참조한다.

 

데이터 영역: 프로그램의 전역변수, 정전변수가 저장되는 공간. 프로그램 시작시 할당되고, 종료시 해제된다.

전역변수를 많이 사용하면 편하지만, 메모리가 부족하다는 단점이 있다.

 

힙(Heap)영역: 사용자(프로그래머)에 의해 관리되는 영역. 즉 프로그래머가 할당, 해제하는 메모리 공간. 런타임 시 크기가 결정되며, 객체타입 (클래스, 객체, 배열) 등이 이에 해당한다.

 

스택 영역: 프로그램이 자동으로 사용하는 임시 메모리 영역. LIFO(Last In First Out)의 특징을 가진다. 함수 호출이 완료되면 할당된 영역이 해제된다.

 


함수 선언식

function sumNumber(a, b) {
	return a + b
}

function getMovieData(searchWord) {
  .....
}

호이스팅의 영향을 받아 함수 선언부가 스코프의 시작점으로 끌어올려진다.

func()            // console.log 출력 됨

function func() {
	console.log('func function call!!!!')
}

 

 

함수 표현식

const multiplyNumber = (a, b) => {
	return a * b
}

const squareNum = function(num) {
	return num * num
}

호이스팅의 영향을 받지 않아서 함수 선언 전에 함수 호출을 할 수 없음.

func()   // 에러 발생 (ReferenceError: Cannot access 'func' before initialization)

const func = () => {
	console.log('func function call!!!!')
}