본문 바로가기
웹 프로그래밍 기초/자바기반의 웹&앱 응용SW 개발자

자바기반의 웹&앱 응용 SW개발자 양성과정 76일차 -113

by oncerun 2020. 7. 2.
반응형

ECMAScript6에서 클래스를 정의하는 방법

class Exam{

	constructor(kor.eng.math){
    
    	this.kor = kor || 0;
        this.eng = eng || 0;
        this.math = math || 0;
       
    }
	
    total(){
    	return this.kor + this.eng + this.math;
    }
    
}

 

이전 ES5에서 생성자를 통해 객체를 생성하려면 다음과 같은 방식으로 했다.

 

function Exam(kor, eng, math){

	this.kor = kor || 0;
    this.eng = eng || 0;
    this.math = math || 0;
}

Exam.prototype.total = function(){
	
    return this.kor + this.eng + this.math;

}

 

두 개의 차이점을 보면 CLASS라는 코드 블록 안에 prototype이 정의되어있다. 따라서 좀 더 외부에 대해 독립성을 가지고 있는 것 같다. 

 

class 블록 안의 코드는 strict 모드로 실행된다.

ES5에 지원된 엄격모드로 느슨했던 문법을 강하게 제어하겠다는 의미입니다.

 

class 블록 안의 모든 메서드는 열거할 수 없다.

열거되기 위한 값으로 사용되지 못합니다.

 

class 생성자는 new 통하지 않고는 호출할 수 없다.

 

 

ES6에서는 상속이가능한 꼼수? 를 제공하고 있습니다.

 

class Exam{

	constructor(kor.eng.math){
    
    	this.kor = kor || 0;
        this.eng = eng || 0;
        this.math = math || 0;
       
    }
	
    total(){
    	return this.kor + this.eng + this.math;
    }
    
}

class NewlecExam extends Exam(){
	constructor(kor = 0, eng =0, math =0, com = 0){
    	super.(kor,eng,math);
        this.com = com
    }
    
    total(){
    	return super.total() + this.com;
    }
    
}


class MyArray extends Array{
			//restarg를 받는 constructor
	constructor(myarg,...arg){
    	super(..arg);//spread하는 arg
        this.myarg = myarg;
    }
}

 

 

 

반응형

댓글