-
[ECMA Script6] 5. 클래스Language/Javascript 2019. 12. 21. 21:47
1. 클래스
ES6 이전에는 클래스가 존재하지는 않았으나, 프로토타입을 이용하여 메서드를 정의해 사용하였습니다.
123456789101112function MyFunc(pName, pVersion){this.pName = pNamethis.pVersion = pVersion}MyFunc.prototype.print = function(){console.log("Name : ", this.pName +', Version : '+this.pVersion)}var func = new MyFunc("테스트","1.0.0.1")func.print() // Name : 테스트, Version : 1.0.0.1cs ES6에는 전통적인 객체지향 언어에서 사용하던 클래스 선언 방식이 가능해졌습니다.
12345678910111213class MyFunc {constructor(pName, pVersion) {this.pName = pNamethis.pVersion = pVersion}print() {console.log(`Name : ${this.pName} , Version : ${this.pVersion}`)}}cs 클래스를 선언 한 다음, new 키워드를 이용하여 새로운 인스턴스를 만들 수 있습니다.
123const func = new MyFunc("클래스","2.1.0.1")func.print() // Name : 클래스, Version : 2.1.0.1cs 클래스 사용이 가능해지면서, 객체지향의 3요소인 상속도 가능해졌습니다.
12345678910111213class SubFunc extends MyFunc {constructor(pSubName,pName, pVersion) {this.pSubName= pSubNamesuper(pName, pVersion)}print() {super.print()console.log(`SubName : ${this.pSubName}`)}}cs 12345const func2 = new SubFunc("서브클래스","클래스","2.1.0.1")func2.print()// Name : 클래스, Version : 2.1.0.1// SubName : 서브클래스cs 'Language > Javascript' 카테고리의 다른 글
[ECMA Script6] 6. 모듈 과 CommonJS (0) 2020.01.04 [ECMA Script6] 4. 스프레드 연산자 (0) 2019.12.14 [ECMA Script6] 3. ES6객체와 배열 (0) 2019.12.07 [ECMA Script6] 2. 화살표 함수(arow function) 및 트랜스파일링 (0) 2019.11.30 JSON 규격 RFC-7159 (Proposed Standard) The JavaScript Object Notation (JSON) Data Interchange Format (0) 2019.07.15 댓글