Class 키워드 이용

class Person{
	constructor(first, last, age, gender, interests){
		this.name = {
			'first': first,
			'last' : last
		}
		this.age = age;
		this.gender = gender;
		this.interests = interests;
	}
    
	greeting(){
		console.log('Hi! I\'m ' + this.name.first + '.');
	}
}

class Teacher extends Person{
	constructor(first, last, age, gender, interests, subject, grade){
		super(first, last, age, gender, interests);
		this.subject = subject;
		this.grade = grade;
	}
}

 

Fuction 키워드 이용
function Person(first, last, age, gender, interests) {
	this.name = {
		'first': first,
		'last' : last
	};
	this.age = age;
	this.gender = gender;
	this.interests = interests;
}

// 메소드 정의
Person.prototype.greeting = function(){
	alert('Hi! I\'m ' + this.name.first + '.');
}

// Person을 상속받는 Teacher
function Teacher(first, last, age, gender, interests, subject){
	Person.call(this, first, last, age, gender, interests);
	this.subject = subject;
}


// 메소드 상속
Teacher.prototype = Object.create(Person.prototype);

// constructor 수정
Teacher.prototype.constructor = Teacher;

'Javascript' 카테고리의 다른 글

[문법] 객체 생성하기  (0) 2019.09.17
[문법] 변수의 전달  (0) 2019.09.16
[DOM 다루기] template 태그 이용  (0) 2019.09.06

+ Recent posts