내배캠 6일차

김민우·2022년 11월 7일
0

자바스크립트 기초 문법에 대해 배웠다.

그 중 Class함수를 배운 것이 기억에 남는다.

class 이름을 선언하여 this를 이용해 내부 값에 다가갈 수 있다.

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

함수 선언과 클래스 선언의 중요한 차이점은 함수의 경우 정의하기 하기 전에 호출할 수 있지만, 클래스는 반드시 정의한 뒤에 사용할 수 있다는 점이다.

const p = new Rectangle(); // ReferenceError
class Rectangle {}

Class 표현식은 이름을 가질 수도 있고, 갖지 않을 수도 있다.

// unnamed
let Rectangle = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};

// named
let Rectangle = class Rectangle2 {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
profile
개발자로서 한걸음

0개의 댓글