클래스

스파르타코딩클럽 JavaScript 문법 뽀개기 수강 중

클래스

class Notebook {
  constructor(name, price, company) {
    this.name = name
    this.price = price
    this.company = company
  }
}
  1. class 키워드와 클래스명
  2. 생성자
  3. this와 속성

객체 생성

const 변수명 = new 클래스명(생성자에서 정의한 매개변수)
const notebook1 = new Notebook('MacBook', 2000000, 'Apple')

메서드

class Product{
    constructor(name, price){
        this.name = name
        this.price = price
    }

    printInfo(){
        console.log(`name: ${this.name}, price ${this.price}`)
    }
}

객체 리터럴

const computer = {
    name: 'Macbook',
    price: 2000000,
    printInfo: function(){
        console.log(`name: ${this.name}, price: ${this.price}`)
    }
}
  • 객체 리터럴을 활용해서 빠르게 객체를 만들 수 있음

배열

배열 선언

const arr1 = new Array(1, 2, 3, 4, 5)
const arr2 = [1, 2, 3, 4, 5]
  1. Array 클래스를 활용한 객체 생성
  2. 배열 바로 생성
const rainbowColors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
console.log(rainbowColors[0])
console.log(rainbowColors.length)
  • 인덱스를 통해 데이터 접근 가능
  • length 속성을 통해 배열 길이 확인

요소 추가/삭제

const rainbowColors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
rainbowColors.push('ultraviolet')
rainbowColors.pop()

[참조] 스파르타코딩클럽 JavaScript 문법 뽀개기

끝!