Search
Duplicate

클래스

생성일
2023/01/23 03:48
태그

클래스

프로토타입 문법을 깔끔하게 작성할 수 있는 Class 문법 도입
Constructor(생성자), Extends(상속) 등을 깔끔하게 처리할 수 있음
코드가 그룹화되어 가독성이 향상됨

과거

var Human = function(type) { this.type = type || 'human'; }; Human.isHuman = function(human) { return human instanceof Human; } Human.prototype.breathe = function() { alert('h-a-a-a=m'); }; var Zero = function(type, firstName, lastName) { Human.apply(this, arguments); this.firstName = firstName; this.lastName = lastName; };
JavaScript
복사

최신

class Human { constructor(type = 'human') { this.type = type; } static isHuman(human) { return human instanceof Human; } breathe() { alert('h-a-a-a-m'); } }
JavaScript
복사

상속 과거

Zero.prototype = Object.create(Human.prototype); Zero.prototype.constructor = Zero; Zero.prototype.sayName = function() { alert(this.firstName + ' ' + this.lastName); }; var oldZero = new Zero('human', 'Zero', 'Cho'); Human.isHuman(oldZero); // true
JavaScript
복사

상속 최신

class Zero extends Human { constructor(type, firstName, lastName) { super(type); // 부모의 함수를 부를 수 있다 this.firstName = firstName; this.lastName = lastName; } sayName() { super.breathe(); alert(`${this.firstName} ${this.lastName}`); } }
JavaScript
복사