더북(TheBook)

최선의 미더운 해결책은 Marsupial을 상속한 Kangaroo 함수를 생성한 뒤 확장하는 것이다.

예제 3-8 고전적 상속을 흉내 내어 Marsupial을 확장

소스 파일 3장\Classical\classical_01.js

function Marsupial(name, nocturnal) {
if (!(this instanceof Marsupial)) {
  throw new Error(“이 객체는 new를 사용하여 생성해야 합니다”);
}
this.name = name;
this.isNocturnal = nocturnal;
}
Marsupial.prototype.isAwake = function(isNight) {
return isNight == this.isNocturnal;
};
 
function Kangaroo(name) {
if (!(this instanceof Kangaroo)) {
  throw new Error(“이 객체는 new를 사용하여 생성해야 합니다”);
}
this.name = name;
this.isNocturnal = false;
}
 
Kangaroo.prototype = new Marsupial();
Kangaroo.prototype.hop = function() {
return this.name + ” 가 껑충 뛰었어요!”;
};
var jester = new Kangaroo(‘제스터’);
console.log(jester.name);
 
var isNightTime = false;
console.log(jester.isAwake(isNightTime)); // true
console.log(jester.hop()); // ‘제스터가 껑충 뛰었어요!’
 
console.log(jester instanceof Kangaroo); // true
console.log(jester instanceof Marsupial); // true

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.