클리엘
CLIEL LAB
클리엘
전체 방문자
오늘
어제
  • 분류 전체보기 (514) N
    • Mobile (47)
      • Kotlin (47)
    • Web (84)
      • NestJS (9)
      • HTML5 & CSS3 (38)
      • Javascript (20)
      • TypeScript (6)
      • JQuery (11)
    • .NET (301) N
      • C# (84) N
      • ASP.NET (67)
      • Windows API for .NET (128)
    • Server (53)
      • SQL Server (10)
      • MariaDB (18)
      • Windows Server (6)
      • node.js (19)
    • System (12)
      • 작업LOG (12)
    • Review (11)
    • ETC (6)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • 블로그 정리

인기 글

태그

  • c#
  • HTML5
  • ASP.NET
  • jQuery
  • Entity Framework
  • 변수
  • Windows API
  • asp.net core web api
  • Kotlin
  • MariaDB
  • android
  • .NET
  • asp.net core
  • JavaScript
  • LINQ
  • CSS3
  • android studio
  • node.js
  • NestJS
  • exception

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
클리엘

CLIEL LAB

Web/Javascript

[javascript] 클래스(Class)

2021. 2. 26. 15:31
728x90

Javascript에서도 Class는 Prototype을 사용하던 이전방식에서 벗어나 좀 더 깔끔하게 코드가 만들어질 수 있도록 해줍니다.

class Car {
    constructor(speed) {
        this.speed = speed;
    }

    currentSpeed()
    {
        return this.speed;
    }

    stop() {
        this.speed = 0;
    }

    static acc(speed) {
        return this.speed + speed;
    }
};

var car = new Car(100);
car.stop();

기존에는 prototype을 통해 함수를 정의하고 직접 속성에 메서드를 할당하는 방식을 따랐지만 필요한 함수는 함수그대로 정의하고 정적 함수의 경우 static을 붙여주는 것만으로 끝낼 수 있습니다.

 

클래스는 정의하는것 뿐아니라 기존 클래스에서 상속해 새로운 클래스를 생성하는 방법 또한 간결해졌습니다.

class Truck extends Car {
    constructor(speed) {
        super(speed);

        this.speed = speed;
    }

    stop()
    {
        super.stop();
    }
}

상속은 extends로 구현되며 super를 사용해 부모의 생성자 함수나 메서드를 호출할 수 있습니다.

 

728x90
저작자표시 비영리 변경금지

'Web > Javascript' 카테고리의 다른 글

[javascript] 모듈(module)  (0) 2021.03.12
[javascript] Promise와 async/await  (0) 2021.02.26
[javascript] 호출스택과 이벤트루프  (0) 2021.02.26
[javascript] 스코프(Scope)와 호이스팅(Hosting)  (0) 2021.02.24
[Javascript] 웹브라우저 저장소  (0) 2020.07.16
    'Web/Javascript' 카테고리의 다른 글
    • [javascript] 모듈(module)
    • [javascript] Promise와 async/await
    • [javascript] 호출스택과 이벤트루프
    • [javascript] 스코프(Scope)와 호이스팅(Hosting)
    클리엘
    클리엘
    누구냐 넌?

    티스토리툴바